🎙 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 5 of 10

What happens if you remove items from an ArrayList inside a for-each loop over it?

What happens if you remove items from an ArrayList inside a for-each loop over it?

  1. A.It works fine
  2. B.ConcurrentModificationException
  3. C.The list becomes empty
  4. D.Compile error
Answer & explanation

Correct answer: ConcurrentModificationException

Modifying the list during for-each iteration causes ConcurrentModificationException. Use removeIf or an Iterator.

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 →