Site icon DataFlair

Quiz on Constructor in Java

quiz on constructor in java

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Constructors are the cornerstones of object creation in Java. Imagine them as the blueprints that come to life when you instantiate a new object.

These special methods, unlike regular methods, don’t have a return type and share the same name as the class itself. Invoked automatically during object creation, constructors are entrusted with a critical task: initializing an object’s state. This initialization process ensures that your objects are born in a valid and consistent condition, ready to fulfill their purpose within your program. By carefully crafting constructors, you lay the foundation for robust and maintainable Java applications.

This DataFlair quiz will put your understanding of constructors to the test, helping you solidify your grasp of this essential concept in object-oriented programming. So, are you ready to dive in and challenge your knowledge?

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    Which of the following keywords is used to create an  object  to invoke a constructor ?

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    class Constructor {

    String a = “DataFlair”;

    public Constructor ( ) {

    System.out.println(a);

    }

    public static void main ( String args [ ] )

    {

    Constructor c = new Constructor();

    }

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    class Demo {

    public Demo ( int a , int b )

    {

    System.out.println( a*b);

    }

    }

    class Main {

    public static void main ( String args [ ] )

    {

    Demo d = new Demo();

    d.Demo(1,10);

    }

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    Which of the following cannot be used in a Constructor ?

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    class Sample {

    static void Sample ( ) {

    System.out.println(“Constructor”);

    }

    public static void main ( String args [ ] )

    {

    Sample s = new Sample();

    s.Sample();

    }

    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    class Sample {

        int a;

    public Example ( int x )

    {

    a=x;

    }

    }

    class Main {

    public static void main ( String args [ ] )

    {

    Sample s = new Sample ( 10 );

    System.out.println(a);

    }

    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

     Which of the following is not a syntax of Constructor ?

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

     class Constructor {

    String a;

    Constructor ( )

    {

    a  = “DataFlair”;

    }

    }

    class Main {

    public static void main ( String args [ ] )

    {

    System.out.println(a);

    Constructor c = new Constructor();

    System.out.println(c.Constructor);

    }

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    class ConstructorSample {

    int a;

    String b;

    public ConstructorSample  ( int c , String d ) 

    {

    a = c;

    b = d;

    }

    void display( )

    {

    System.out.println(a );

    System.out.println(b);

    }

    }

    class Main {

    public static void main ( String args [ ] )

    {

    ConstructorSample s = new ConstructorSample(1,One);

    s.display();

    }

    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    Which of the following  methods can have different arguments using constructors ?

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    class Function {

    int p;

    Function ( ) 

    {

    p=100;

    }

    public static void main ( String args [ ] )

    {

    Function f = new Function();

    System.out.println(f.Function);

    }

    }

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    class Main {

    Main ( )

    {

    System.out.println(“Default Constructor”);

    }

    Main ( int x )

    {

    this(1);

    System.out.println(x);

    }

    }

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    Which of the following is used while using Constructor ?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

     class ConstructorOverloading {

    int x , y , z ;

    ConstructorOverloading ( int a , int b )

    {

    x=a;

    y=b;

    System.out.println(x+y);

    }

    ConstructorOverloading ( int a , int b , int c )

    {

    x=a;

    y=b;

    z=c;

    System.out.println(a+b+c);

    }

    public static void main ( String args [ ] )

    {

    ConstructorOverloading c = new ConstructorOverloading( 2 , 4 , 8 );

    }

    }

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    class Example {

    public Example {

    System.out.println(a);

    }

    public static void main ( String args [ ] )

    {

    Example e = new Example();

    e.Example();

    }

    }

    Correct
    Incorrect

Summary:

Have you just conquered the DataFlair quiz on Constructors in Java? If so, congratulations! This quiz was designed to test your understanding of the fundamentals of constructors, including the keywords used to create objects (`new`) and invoke constructors themselves.

By tackling these questions, you’ve taken a significant step towards mastering this essential concept in Java. But your journey with constructors doesn’t end here!

There’s a whole world of constructors waiting to be explored. Delve deeper into resources that cover advanced constructor types, such as default constructors (automatically generated by the compiler if you don’t define your own) and parameterized constructors (allowing you to provide initial values for object properties during creation).

Additionally, explore best practices for constructor design to ensure your objects are consistently initialized in a robust and efficient way. Remember, the more you practice, the more comfortable you’ll become with constructors. So keep at it, experiment with different scenarios, and watch your Java programming skills flourish!

Exit mobile version