ProgrammingModerateProduct-company placement · Java

What happens when this is compiled?

What happens when this is compiled?
class P { P(int x) { } P() { } }
class C extends P { }
// in main:
C c = new C(5);
  1. A. It compiles and calls P(int)
  2. B. It compiles and calls P() then ignores 5
  3. C. Runtime error: no matching constructor
  4. D. Compile-time error: C has no constructor taking an int

Correct answer: D. Compile-time error: C has no constructor taking an int

Constructors are not inherited. C only gets the compiler-supplied default constructor C(), which calls super().

More OOP with C++ and Java questions

Exams that ask this topic