ProgrammingVery hardGATE CS · TCS Digital · Advanced

What is printed?

What is printed?
static void f(long x)    { System.out.print("L"); }
static void f(Integer x) { System.out.print("I"); }
static void f(Object x)  { System.out.print("O"); }
static void f(int... x)  { System.out.print("V"); }
// in main:
f(5);
f(Integer.valueOf(5));
f('a');
f(5.0);
  1. A. OIOO
  2. B. LILO
  3. C. LIOO
  4. D. LIIO

Correct answer: B. LILO

Overload resolution tries subtyping/widening only first: f(5) and f('a') widen to long; the Integer argument matches f(Integer) exactly. For 5.0 no widening works, so the second phase (boxing to Double, then Object) selects f(Object).

More OOP with C++ and Java questions

Exams that ask this topic