ProgrammingVery hardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
// static methods of the main class:
static void g(long l) { }
static void h(Long l) { }
// in main:
g(Integer.valueOf(5));   // L1
h(5);                    // L2
h(5L);                   // L3
  1. A. L1 only
  2. B. L1 and L2
  3. C. None
  4. D. L2 only

Correct answer: D. L2 only

Unboxing followed by widening is allowed (Integer → int → long). But an int can only box to Integer, and Integer is not a Long, so h(5) has no applicable method.

More OOP with C++ and Java questions

Exams that ask this topic