ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class P {
    static void sm() { System.out.print("P"); }
    void im()        { System.out.print("P"); }
}
class C extends P {
    static void sm() { System.out.print("C"); }
    void im()        { System.out.print("C"); }
}
// in main:
P p = new C();
p.sm();
p.im();
  1. A. PC
  2. B. CC
  3. C. PP
  4. D. CP

Correct answer: A. PC

Static methods are not overridden, only hidden: p.sm() is bound at compile time by the declared type P. Instance methods are dispatched on the actual object, a C. So PC.

More OOP with C++ and Java questions

Exams that ask this topic