🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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");
        }
    }
}
  1. A.[a, b, c]
  2. B.immutable
  3. C.[a, b]
  4. 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 →