nerdexam
Oracle

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.

Working with Inheritance

Question

Given:

Which constructor initializes the variable x3?

Exhibit

1Z0-803 question #213 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)
  • A
    17% (9)
  • B
    8% (4)
  • C
    72% (38)
  • D
    4% (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.

AOnly the default constructor of class X

The default constructor of class X (a superclass) is responsible for initializing variables declared in X, not variables declared in its subclasses like Z.

BOnly the no-argument constructor of class Y

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.

COnly the no-argument constructor of class ZCorrect

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.

DOnly the default constructor of object class

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

#constructors#constructor chaining#inheritance

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice