ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
List<Integer> l = Arrays.asList(3, 1, 2); Collections.sort(l); System.out.print(l + " "); l.clear();
- A. "[1, 2, 3] " is printed, then UnsupportedOperationException is thrown
- B. [1, 2, 3] []
- C. UnsupportedOperationException is thrown by sort
- D. "[3, 1, 2] " is printed, then UnsupportedOperationException is thrown
Correct answer: A. "[1, 2, 3] " is printed, then UnsupportedOperationException is thrown
The list from Arrays.asList is fixed-size but writable: sorting only sets elements, so it works. Any size change (add, remove, clear) throws UnsupportedOperationException.
More OOP with C++ and Java questions
- What is printed? java List<String> l = new ArrayList<>(List.of("a", "b", "c")); ListIterator<String> it = l.listIterator(); while…
- What is printed? java System.out.print("a.b.c".split(".").length + " " + "a.b.c".split(".").length);
- What is printed? java System.out.print("a.b".replace(".", "x") + " " + "a.b".replaceAll(".", "x"));
- What is printed? java List<Integer> src = List.of(1, 2); List<Integer> dst = new ArrayList<>(5); Collections.copy(dst, src)…
- 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[]));