ProgrammingVery hardTCS NQT Prime · Infosys SP

What is printed?

What is printed?
static void f(Integer i) { System.out.print("I"); }
static void f(long l)    { System.out.print("L"); }
static void f(Object o)  { System.out.print("O"); }
static void f(int... a)  { System.out.print("V"); }
// main:
f(5); f(Integer.valueOf(5)); f((short) 5); f(5, 6);
  1. A. LILV
  2. B. IIIV
  3. C. OIOV
  4. D. LIOV

Correct answer: A. LILV

Phase 1 (no boxing) picks f(long) for int and short; Integer matches f(Integer) as most specific over f(Object); two ints can only match the varargs method.

More OOP with C++ and Java questions

Exams that ask this topic