Java SE 7 Programmer I — Question 31
Given:
class X {}
class Y { Y ( ) { } }
class Z { Z (int i ) { } }
Which class has a default constructor?
Answer options
- A. X only
- B. Y only
- C. Z only
- D. X and Y
- E. Y and Z
- F. X and Z
- G. X, Y and Z
Correct answer: A
Explanation
Class X has a default constructor because it does not define any constructors, thus Java provides one automatically. Class Y has a default constructor since it explicitly defines one without parameters. However, class Z does not have a default constructor because it requires an integer parameter, making option A the only correct choice.