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);
  1. A. [3, 4, 5, 6] then IllegalArgumentException
  2. B. [3, 4, 5] and 8 is added to s
  3. C. "[3, 4, 5] " is printed, then IllegalArgumentException is thrown
  4. 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

Exams that ask this topic