ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
class P { String f = "P"; static String s() { return "P"; } String i() { return "P"; } }
class C extends P { String f = "C"; static String s() { return "C"; } String i() { return "C"; } }
// in main:
P p = new C();
System.out.print(p.s() + p.i() + p.f);
  1. A. CCC
  2. B. PCP
  3. C. PCC
  4. D. CCP

Correct answer: B. PCP

Only instance methods are overridden. Static methods are hidden and chosen by the static type P, and fields are also resolved by the static type, so s() and f give P while i() dispatches to C.

More OOP with C++ and Java questions

Exams that ask this topic