ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
TreeSet<Integer> s = new TreeSet<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); SortedSet<Integer> sub = s.subSet(3, 6); System.out.print(sub + " "); sub.add(8);
- A. [3, 4, 5, 6] then IllegalArgumentException
- B. [3, 4, 5] and 8 is added to s
- C. "[3, 4, 5] " is printed, then IllegalArgumentException is thrown
- D. "[3, 4, 5] " is printed, then UnsupportedOperationException is thrown
Correct answer: C. "[3, 4, 5] " is printed, then IllegalArgumentException is thrown
subSet(from, to) is a view of [3, 6), exclusive of 6. The view accepts additions only inside its range; inserting 8 throws IllegalArgumentException (key out of range).
More OOP with C++ and Java questions
- What is printed? java List<String> l = Arrays.asList("b", null, "a"); l.sort(Comparator.nullsFirst(Comparator.naturalOrder()))…
- What is printed? java // static method of the main class: static String make() { System.out.print("M"); return "y"; } // in main…
- What is printed? java Stream<Integer> s = Stream.of(1, 2, 3); System.out.print(s.count()); System.out.print(s.count());
- What happens when this method is compiled (Java 7 or later)? java static void f() throws java.io.IOException { try { if (Math.random() <…
- What is printed? java try { System.out.print("T"); System.exit(0); } finally { System.out.print("F"); }
- What is printed? java class N { public Object clone() throws CloneNotSupportedException { return super.clone(); } } // in main: try { new…