Skip to content
Sharp

Collections · interview question

After iterator removal, what remains in the fruit list?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsiteration-and-traversal
java
List<String> fruits = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
    if (iterator.next().equals("banana")) {
        iterator.remove();
    }
}
System.out.println(fruits);
Choose one answer
Answer and explanation

Correct answer

D. ["apple", "cherry"]

Why

The code correctly uses the iterator's remove method to safely remove the element "banana" from the list during iteration.

Test yourself on Collections

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

Start free test
All Collections questions →