ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
// static methods of the main class:
static void f(int... a)           { System.out.print("V"); }
static void f(Integer a, Integer b) { System.out.print("II"); }
// in main:
f(1, 2);
f(1, 2, 3);
  1. A. VV
  2. B. IIII
  3. C. IIV
  4. D. Compile-time error: ambiguous call f(1, 2)

Correct answer: C. IIV

Overload resolution tries boxing (phase 2) before varargs (phase 3), so f(1, 2) picks f(Integer, Integer). Three arguments fit only the varargs method.

More OOP with C++ and Java questions

Exams that ask this topic