Java OCA OCP Practice Question 326

Question

Which of the following are true about the "default" constructor?

Select 2 options

  • A. It is provided by the compiler only if the class does not define any constructor.
  • B. It initializes the instance members of the class.
  • C. It calls the default 'no-args' constructor of the super class.
  • D. It initializes instance as well as class fields of the class.
  • E. It is provided by the compiler if the class does not define a 'no-args' constructor.


Correct Options are  : A C

Note

The "default" constructor is not provided even if the class declares any other constructor.

The default constructor is provided by the compiler only when a class does not define ANY constructor explicitly.

For example,

public class A { 
  //This constructor is automatically 
  //inserted by the compiler      
  public A ()  //w ww  .  j  a v  a 2 s .com
    //Note that it calls the super class' 
    //default no-args constructor
    super ();  
   } 
} 
public class A { 
  //Compiler will not generate any constructor 
  //because the programmer has d 
  public A (int i){ 
     //do something 
   } 
} 



PreviousNext

Related