nerdexam
Oracle

1Z0-809 · Question #143

Given: ``java class Vehicle { String type; int maxSpeed = 100; Vehicle(String type, int maxSpeed) { this.type = type; this.maxSpeed = maxSpeed; } } class Car extends Vehicle { String trans…

The correct answer is B. 4W 150 Manual. There is an error in the stated correct answer. Based on Java semantics, option C is the actual correct answer - compilation fails at both n1 and n2. Here is why: Why n1 fails: Car(String trans) contains no explicit super() or this() call, so the compiler inserts an implicit…

Question

Given:
class Vehicle {
 String type;
 int maxSpeed = 100;
 Vehicle(String type, int maxSpeed) {
 this.type = type;
 this.maxSpeed = maxSpeed;
 }
}
class Car extends Vehicle {
 String trans;
 Car(String trans) { //line n1
 this.trans = trans;
 }
 Car(String type, int maxSpeed, String trans) { //line n2
 super(type, maxSpeed);
 this(trans);
 }
}
And given the code fragment:
7. Car c1 = new Car("Auto");
8. System.out.println(c1.type + " " + c1.maxSpeed + " " + c1.trans);
9. Car c2 = new Car("Manual", 150, "Manual");
10. System.out.println(c2.type + " " + c2.maxSpeed + " " + c2.trans);
What is the output?

Options

  • Anull 0 Auto
  • B4W 150 Manual
  • CCompilation fails at both line n1 and line n2.
  • DCompilation fails only at line n1.
  • ECompilation fails only at line n2.

How the community answered

(34 responses)
  • A
    6% (2)
  • B
    76% (26)
  • C
    3% (1)
  • D
    3% (1)
  • E
    12% (4)

Explanation

There is an error in the stated correct answer. Based on Java semantics, option C is the actual correct answer - compilation fails at both n1 and n2. Here is why:

Why n1 fails: Car(String trans) contains no explicit super() or this() call, so the compiler inserts an implicit super() (no-arg). But Vehicle only declares the explicit constructor Vehicle(String type, int maxSpeed), which suppresses the default no-arg constructor. There is nothing to call, so compilation fails here.

Why n2 fails: Car(String type, int maxSpeed, String trans) calls super(type, maxSpeed) and then this(trans). Java requires that an explicit constructor invocation (super(...) or this(...)) must be the first and only such call in a constructor body. You cannot chain both in the same constructor - that is a compile-time error.

Why the other options are wrong:

  • A would require n1 to compile successfully, which it cannot.
  • B is doubly wrong: it requires compilation to succeed, and "4W" doesn't appear anywhere in the code.
  • D has it backwards - n1 and n2 both fail, and the reason n1 fails is the missing no-arg Vehicle constructor.
  • E is wrong because n1 also fails for the same reason (missing no-arg parent constructor).

Memory tip: A constructor can delegate via super(...) or this(...) - never both in the same constructor. And any class with only explicit constructors loses its implicit no-arg constructor, which breaks any subclass that tries to call it implicitly.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice