1Z0-803 · Question #213
Given: Which constructor initializes the variable x3?
The correct answer is C. Only the no-argument constructor of class Z. The variable x3 is initialized by the constructor of the class in which it is declared, as that class's constructor is responsible for setting up its own instance variables.
Question
Given:
Which constructor initializes the variable x3?
Exhibit
Options
- AOnly the default constructor of class X
- BOnly the no-argument constructor of class Y
- COnly the no-argument constructor of class Z
- DOnly the default constructor of object class
How the community answered
(53 responses)- A17% (9)
- B8% (4)
- C72% (38)
- D4% (2)
Why each option
The variable `x3` is initialized by the constructor of the class in which it is declared, as that class's constructor is responsible for setting up its own instance variables.
The default constructor of class X (a superclass) is responsible for initializing variables declared in X, not variables declared in its subclasses like Z.
The no-argument constructor of class Y (an intermediate superclass) is responsible for initializing variables declared in Y, not variables declared in its subclass Z.
The constructor of class Z is responsible for initializing variables declared within class Z, including `x3`. Whether `x3` is initialized with a default value, an explicit initializer at declaration, or an assignment within the constructor body, this process occurs during the execution of Z's constructor.
The default constructor of the `Object` class (the root class) initializes fundamental object properties common to all objects but has no knowledge of specific instance variables like `x3` defined in a subclass.
Concept tested: Java inheritance, constructor chaining, field initialization
Source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-8.html#jls-8.8
Topics
Community Discussion
No community discussion yet for this question.
