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- A. L1 only
- B. L1 and L2
- C. None
- 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
- Which lines fail to compile? java String s = "x"; Number n = 1; Runnable r = () -> { }; Object o = s; boolean a = s instanceof Integer; //…
- What is printed? java // Java 16 or later record R(int[] a) { } record S(List<Integer> l) { } // in main: System.out.print(new R(new…
- What is printed? java enum Op { ADD { int apply(int a, int b) { return a + b; } }, MUL { int apply(int a, int b) { return a b; } }…
- What is printed? java enum Day { MON, TUE, WED } // in main: Map<Day, Integer> m = new EnumMap<>(Day.class); m.put(Day.WED, 3)…
- Which line fails to compile? java List<String>[] a = new List<String>[2]; // L1 List<?>[] b = new List<?>[2]; // L2 List<String>[] c = new…
- Which line fails to compile? java class Box<T extends Number> { double d(T t) { return t.doubleValue(); } } // in main: Box<Integer> a =…