ProgrammingHardTCS NQT · Infosys pattern

Which statement about this code is correct?

Which statement about this code is correct?
class P {
    P(int x) { System.out.print("P" + x + " "); }
}
class C extends P {
    C() { System.out.print("C "); }
}
  1. A. It fails to compile: C()'s implicit super() call needs a no-argument P constructor, which doesn't exist
  2. B. It compiles because Java always supplies a default super constructor with zero-initialized arguments
  3. C. It compiles and prints 'P0 C ' when new C() runs
  4. D. It compiles, but calling new C() throws a runtime NoSuchMethodError

Correct answer: A. It fails to compile: C()'s implicit super() call needs a no-argument P constructor, which doesn't exist

Every constructor's first statement is implicitly super() unless written otherwise. P only declares P(int), not a no-arg constructor, so C()'s implicit super() fails to resolve — a compile-time error, not a runtime one.

More OOP with C++ and Java questions

Exams that ask this topic