ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
// static methods of the main class:
static void f(Object o) { System.out.print("O"); }
static void f(int... a)  { System.out.print("V"); }
static void g(long x)    { System.out.print("L"); }
static void g(int... a)  { System.out.print("V"); }
// in main:
f(5);
g(5);
g();
  1. A. VLV
  2. B. OVV
  3. C. VVV
  4. D. OLV

Correct answer: D. OLV

Varargs are tried only in the last phase. f(5) boxes to Integer and matches f(Object) in phase 2 (O); g(5) widens to long in phase 1 (L). g() has no fixed-arity candidate, so it uses varargs (V).

More OOP with C++ and Java questions

Exams that ask this topic