ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
List<Integer> src = List.of(1, 2); List<Integer> dst = new ArrayList<>(5); Collections.copy(dst, src); System.out.print(dst);
- A. [1, 2]
- B. [1, 2, null, null, null]
- C. An IndexOutOfBoundsException is thrown
- D. []
Correct answer: C. An IndexOutOfBoundsException is thrown
Collections.copy overwrites existing elements and requires dst.size() ≥ src.size(). The constructor argument 5 is only a capacity, so dst has size 0.
More OOP with C++ and Java questions
- What is printed? java enum Size { S; public String toString() { return "small"; } } // in main: System.out.print(Size.S + " " +…
- What is printed? java Object o = new int[]{1}; System.out.print((o instanceof int[]) + " " + (o instanceof Object[]));
- Which line fails to compile? java class P { void run() throws java.io.IOException { } } class C extends P { @Override void run() { } } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); m.keySet().remove("a"); System.out.print(m.size() + " ")…
- What is printed? java TreeSet<Integer> s = new TreeSet<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); SortedSet<Integer> sub = s.subSet(3, 6)…
- What is printed? java List<String> l = Arrays.asList("b", null, "a"); l.sort(Comparator.nullsFirst(Comparator.naturalOrder()))…