ProgrammingModerateWipro NLTH · Accenture

What is printed?

What is printed?
class P { static String w() { return "P"; } }
class Q extends P { static String w() { return "Q"; } }
// in main:
P p = new Q();
System.out.print(p.w());
  1. A. Q
  2. B. P
  3. C. PQ
  4. D. Compile error

Correct answer: B. P

Static methods are hidden, not overridden; the call is bound at compile time using the declared type P.

More OOP with C++ and Java questions

Exams that ask this topic