ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6)); l.subList(1, 4).clear(); System.out.print(l);
- A. [1, 6]
- B. [5, 6]
- C. [1, 5, 6]
- D. [1, 2, 6]
Correct answer: C. [1, 5, 6]
subList(1, 4) is a view of indices 1, 2, 3 (end exclusive) = 2, 3, 4; clearing the view removes them from the backing list.
More OOP with C++ and Java questions
- What is printed? java List<String> a = Arrays.asList("x", null); System.out.print(a.size() + " "); List<String> b = List.of("x", null)…
- Which lines fail to compile? java List<Integer> ints = new ArrayList<>(); List<Number> nums = ints; // L1 List<? extends Number> ext =…
- Which lines fail to compile? java List<? super Integer> l = new ArrayList<Number>(); l.add(1); // L1 l.add(2.5); // L2 Object o =…
- 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);