ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
// static methods of the main class:
static void f(long x)    { System.out.print("L"); }
static void f(Integer x) { System.out.print("I"); }
static void f(int... x)  { System.out.print("V"); }
static void g(Long x)    { System.out.print("G"); }
static void g(Object x)  { System.out.print("O"); }
// in main:
f(5);
g(5);
  1. A. IG
  2. B. LG
  3. C. LO
  4. D. IO

Correct answer: C. LO

Phase 1 of overload resolution allows widening but not boxing, so f(long) wins over f(Integer). For g, int cannot widen to Long and boxing gives Integer, which is not a Long; Integer is an Object, so g(Object) is chosen.

More OOP with C++ and Java questions

Exams that ask this topic