ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

java void f(int x) { System.out.print("int"); } void f(Integer x) { System.out.print("Integer"); } void f(Object... x) { System.out.print("varargs"); } //…

void f(int x) { System.out.print("int"); }
void f(Integer x) { System.out.print("Integer"); }
void f(Object... x) { System.out.print("varargs"); }
// main: f(5);
What is printed?
  1. A. int
  2. B. Compile error: ambiguous
  3. C. varargs
  4. D. Integer

Correct answer: A. int

Java's overload resolution first looks for a match using only widening (no boxing, no varargs); f(int) matches 5 directly with no boxing needed, so it is chosen ahead of the autoboxed f(Integer) and the varargs f(Object...).

More OOP with C++ and Java questions

Exams that ask this topic