nerdexam
Oracle

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.

Java Basics

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)
  • A
    89% (51)
  • B
    4% (2)
  • C
    2% (1)
  • G
    5% (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.

AX onlyCorrect

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.

BY only

Class Y explicitly defines a no-argument constructor Y(), which prevents the compiler from generating a default constructor.

CZ only

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.

DX and Y

Classes Y and Z explicitly define constructors, therefore the compiler does not provide a default constructor for either.

EY and Z

Classes Y and Z explicitly define constructors, therefore the compiler does not provide a default constructor for either.

FX and Z

Class Z explicitly defines a constructor, so it does not have a default constructor.

GX, Y and Z

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

#constructors#default constructor#class definition

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice