← Quiz: Java Collections (List and Set): all 10 questions
Quiz: Java Collections (List and Set) · Question 6 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> fixed = List.of("a", "b");
try {
fixed.add("c");
} catch (UnsupportedOperationException e) {
System.out.println("immutable");
}
}
}
- A.[a, b, c]
- B.immutable
- C.[a, b]
- D.Compile error
Answer & explanation
Correct answer: immutable
List.of creates an unmodifiable list. Wrap it, as in new ArrayList<>(List.of(...)), when you need to change it.
Can you get the rest right without peeking?
Take the timed Quiz: Java Collections (List and Set) quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →