ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

What is printed?
class P {
    P() { show(); }
    void show() { System.out.print("P "); }
}
class C extends P {
    int x = 5;
    final int y = 7;
    C() { show(); }
    void show() { System.out.print(x + "" + y + " "); }
}
// in main:
new C();
  1. A. 00 57
  2. B. P 57
  3. C. 57 57
  4. D. 07 57

Correct answer: D. 07 57

P's constructor runs before C's field initialisers, and the overridden show() already dispatches to C: x is still 0. y is a final field initialised with a constant, so it is a constant variable and every use of it is the compile-time value 7.

More OOP with C++ and Java questions

Exams that ask this topic