ProgrammingVery hardProduct-company placement · Java
Which lines fail to compile?
Which lines fail to compile?
List<? super Integer> l = new ArrayList<Number>(); l.add(1); // L1 l.add(2.5); // L2 Object o = l.get(0); // L3 Integer i = l.get(0); // L4
- A. L4 only
- B. L2 and L4
- C. L1 and L2
- D. L2, L3 and L4
Correct answer: B. L2 and L4
? super Integer accepts Integer (and subtypes) as input, so L1 compiles but a Double (L2) does not. Reading gives only Object, so L3 is fine and L4 needs a cast. (PECS: producer extends, consumer super.)
More OOP with C++ and Java questions
- What is printed? java Object[] arr = new String[2]; arr[0] = "ok"; try { arr[1] = 42; System.out.print("stored"); } catch…
- What is printed? java int[] a = {1, 2}, b = {1, 2}; int[][] p = {{1}}, q = {{1}}; System.out.print(a.equals(b) + " " + Arrays.equals(a, b)…
- What is printed? java String s = null; s += "x"; String t = "a" + null + 1 + 2; System.out.print(s + " " + t);
- What is printed? java StringBuilder sb = new StringBuilder("abcdef"); sb.insert(2, "XY").delete(5, 7).reverse().setCharAt(0, 'Z')…
- What is printed? java String s = "level"; String t = s.replace('l', 'p'); System.out.print(t + " " + (s.replace('z', 'q') == s) + " " + s);
- What is printed? java String s = "hello"; System.out.print("[" + s.substring(5) + "]"); System.out.print(s.substring(6));