Java OCA OCP Practice Question 1329

Question

Which of the following statements about no-argument constructors is correct?

If a parent class

  • A. does not include a no-argument constructor, a child class cannot declare one.
  • B. does not include a no-argument constructor nor a default one inserted by the compiler, a child class must contain at least one constructor definition.
  • C. contains a no-argument constructor, a child class must contain a no-argument constructor.
  • D. contains a no-argument constructor, a child class must contain at least one constructor.


B.

Note

If a parent class does not include a no-argument constructor, a child class can still explicitly declare one; it just has to call an appropriate parent constructor with super().

Option A is incorrect.

If a parent class does not include a no-argument constructor, the child class must explicitly declare a constructor, since the compiler will not be able to insert the default no-argument constructor.

Option B is correct.

Option C is incorrect because a parent class can have a no-argument constructor, while its subclasses do not.

If Option C was true, then all classes would be required to have no-argument constructors since they all extend java.lang.Object, which has a no-argument constructor.

Option D is incorrect.

The default no-argument constructor can be inserted into any class that directly extends a class that has a no-argument constructor.

No constructors in the subclass are required.




PreviousNext

Related