ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
class P { void f(int x)  { System.out.print("P-int "); } }
class C extends P { void f(long x) { System.out.print("C-long "); } }
// in main:
P p = new C(); p.f(5);
C c = new C(); c.f(5);
  1. A. C-long C-long
  2. B. P-int C-long
  3. C. C-long P-int
  4. D. P-int P-int

Correct answer: D. P-int P-int

C.f(long) has a different parameter type, so it OVERLOADS rather than overrides f(int). Through P only f(int) exists; through C both are visible and f(int) is the exact match for an int argument.

More OOP with C++ and Java questions

Exams that ask this topic