ProgrammingHardAccenture · Cognizant pattern

What is printed?

What is printed?
List<Integer> l = Arrays.asList(1, 2, 3);
try {
    l.set(0, 9);
    System.out.print(l);
    l.add(4);
} catch (UnsupportedOperationException e) {
    System.out.print(" U");
}
  1. A. [9, 2, 3] U
  2. B. U
  3. C. [9, 2, 3, 4]
  4. D. [9, 2, 3]

Correct answer: A. [9, 2, 3] U

Arrays.asList returns a fixed-size list backed by the array: set works because it does not change the size, but add throws UnsupportedOperationException. So the list prints, then U.

More OOP with C++ and Java questions

Exams that ask this topic