Object-Oriented Programming · Question 27 · Hard

What is the output of the following Java code?

static void m(long x)    { System.out.println("long"); }
static void m(Integer x) { System.out.println("Integer"); }

// in main:
m(5);
  1. Integer
  2. Compilation error: ambiguous call
  3. ClassCastException at runtime
  4. long
Answer and explanation

Answer: D. long

Explanation: Java resolves overloads in phases: it first looks for a match using only widening, without boxing. int → long widening matches m(long), so it is chosen before boxing to Integer is even considered.

Previous: A House is made of Rooms, and the Rooms cannot exist without the House: when… · Next: What is the output of the following C++ program?

Practise object-oriented programming

Try a fresh set of object-oriented programming questions with an explanation after every answer, free and without signing up: Object-Oriented Programming practice. Or sit a full placement mock test.

More object-oriented programming questions

More technical & programming practice

DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge