1Z0-803 · Question #22
Given: class X {} class Y {Y () {}} class Z {z(int i ) {} } Which class has a default constructor?
The correct answer is A. X only. Only class X has a default constructor, as it does not explicitly define any constructor, prompting the Java compiler to automatically provide a public, no-argument constructor.
Question
Given:
class X {} class Y {Y () {}} class Z {z(int i ) {} } Which class has a default constructor?
Options
- AX only
- BY only
- CZ only
- DX and Y
- EY and Z
- FX and Z
- GX, Y and Z
How the community answered
(57 responses)- A89% (51)
- B4% (2)
- C2% (1)
- G5% (3)
Why each option
Only class X has a default constructor, as it does not explicitly define any constructor, prompting the Java compiler to automatically provide a public, no-argument constructor.
Class X does not declare any constructors, so the Java compiler automatically synthesizes a public, no-argument default constructor for it, making it the only class among the options to possess one.
Class Y explicitly defines a no-argument constructor Y(), which prevents the compiler from generating a default constructor.
Class Z explicitly defines a constructor z(int i) that takes an argument, which also prevents the compiler from generating a default no-argument constructor.
Classes Y and Z explicitly define constructors, therefore the compiler does not provide a default constructor for either.
Classes Y and Z explicitly define constructors, therefore the compiler does not provide a default constructor for either.
Class Z explicitly defines a constructor, so it does not have a default constructor.
Classes Y and Z explicitly define constructors, so they do not have default constructors.
Concept tested: Java default constructor generation rules
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Topics
Community Discussion
No community discussion yet for this question.