← 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?
- A.It works fine
- B.ConcurrentModificationException
- C.The list becomes empty
- 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 →