ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
class P {
    private void f() { System.out.print("P"); }
    void call() { f(); }
}
class C extends P {
    void f() { System.out.print("C"); }
}
// in main:
new C().call();
  1. A. C
  2. B. P
  3. C. Compile error: f() has weaker access
  4. D. PC

Correct answer: B. P

A private method is not inherited and cannot be overridden; C.f is a new, unrelated method. Inside P, f() binds to P's private f, so P is printed.

More OOP with C++ and Java questions

Exams that ask this topic