ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
// static methods of the main class:
static void f(long x)    { System.out.print("long"); }
static void f(Integer x) { System.out.print("Integer"); }
static void f(Object x)  { System.out.print("Object"); }
static void f(int... x)  { System.out.print("varargs"); }
// in main:
f(5);
  1. A. long
  2. B. varargs
  3. C. Integer
  4. D. Object

Correct answer: A. long

Java resolves overloads in phases: first by widening only (int to long), then with boxing, then with varargs. f(long) is found in phase one, so boxing to Integer or Object is never considered.

More OOP with C++ and Java questions

Exams that ask this topic