A developer writes code to iterate over a list of strings and print each element. The code uses an enhanced for loop. Which statement is true about the enhanced for loop?
Trap 1: It can only be used with arrays.
It can also be used with collections that implement Iterable.
Trap 2: It requires an explicit counter variable.
The enhanced for loop hides the counter variable.
Trap 3: It allows removing elements from the collection during iteration…
Removing elements during iteration with enhanced for loop causes ConcurrentModificationException.
- A
It can be used with arrays and any object that implements Iterable.
The enhanced for loop works on arrays and Iterable objects.
- B
It can only be used with arrays.
Why wrong: It can also be used with collections that implement Iterable.
- C
It requires an explicit counter variable.
Why wrong: The enhanced for loop hides the counter variable.
- D
It allows removing elements from the collection during iteration without ConcurrentModificationException.
Why wrong: Removing elements during iteration with enhanced for loop causes ConcurrentModificationException.