Skip to content
Sharp

Collections · interview question

What happens if you try to remove an element from a List while iterating over it using an enhanced for loop?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsiteration-and-traversal
Choose one answer
Answer and explanation

Correct answer

C. It throws a ConcurrentModificationException.

Why

Removing elements during iteration with an enhanced for loop causes a ConcurrentModificationException because the iterator detects changes to the collection's structure.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →