🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Immutability and Value Objects: all 10 questions

Quiz: Immutability and Value Objects · Question 3 of 10

Which line correctly makes a defensive, unmodifiable copy of a list parameter?

Which line correctly makes a defensive, unmodifiable copy of a list parameter?

  1. A.this.items = items;
  2. B.this.items = (List<String>) items.clone();
  3. C.this.items = List.copyOf(items);
  4. D.this.items = final items;
Answer & explanation

Correct answer: this.items = List.copyOf(items);

List.copyOf creates a new unmodifiable list, so later changes to the caller's list do not leak in.

Can you get the rest right without peeking?

Take the timed Quiz: Immutability and Value Objects quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →