Skip to content
Sharp

Collections · interview question

In which scenario is using an iterator preferred over an enhanced for loop?

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

Correct answer

A. Traversing and modifying a list

Why

An iterator is preferred when you need to traverse and modify a list at the same time, as it provides a safe way to remove elements without causing a ConcurrentModificationException.

Test yourself on Collections

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

Start free test
All Collections questions →