Given:
List<String> list1 = new ArrayList<>(); list1.add("A"); list1.add("B");
List list2 = List.copyOf(list1); list2.add("C");
List<List<String>> list3 = List.of(list1, list2); System.out.println(list3);
What is the result?
A. [[A, B],[A, B]]
B. An exception is thrown at run time.
C. [[A, B], [A, B, C]]
D. [[A, B, C], [A, B, C]]
B)
The List.of and List.copyOf static factory methods provide a convenient way to create unmodifiable lists. The List instances created by these methods have the following characteristics:
They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List’s contents to appear to change.
https://docs.oracle.com/javase/10/docs/api/java/util/List.html