ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// static methods of the main class:
static void f(double d) { System.out.print("D"); }
static void f(Object o) { System.out.print("O"); }
// in main:
Integer x = 5;
f(5);
f(x);
  1. A. DO
  2. B. DD
  3. C. OO
  4. D. OD

Correct answer: A. DO

Phase 1 allows only widening and subtyping, no boxing or unboxing. int 5 widens to double. Integer x is already an Object (subtype), while reaching double would need unboxing, so f(Object) wins.

More OOP with C++ and Java questions

Exams that ask this topic