ProgrammingHardProduct-company MCQ pattern

What is printed?

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

Correct answer: C. P

Private methods are not inherited and cannot be overridden; C.f is an unrelated new method. Inside P, the call f() is bound to P's private f at compile time, so P prints.

More OOP with C++ and Java questions

Exams that ask this topic