Iterator & Iterable — Concept Check
Questions on Iterator & Iterable. Implement Iterable to enable enhanced for-each on your own classes, understand Iterator fail-fast behaviour, and use ListIterator for bidirectional traversal.
Questions in this quiz
8 questions on Iterator & Iterable — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer is iterating over an ArrayList and modifies it using the list's remove method during a for-each loop. What is likely to happen?
- AThe iteration continues without issues
- BThe list is modified safely
- CConcurrentModificationException is thrown
- DThe loop skips the modified element
2.In Java, what happens if you call next() on an Iterator when there are no more elements?
- AThe iterator resets to the beginning
- BNoSuchElementException is thrown
- CThe method returns null
- DThe last element is returned again
3.A developer uses a ListIterator to traverse a LinkedList. What advantage does this provide?
- ABidirectional traversal and in-place modification
- BSimplified syntax for iteration
- CFail-fast behavior to prevent errors
- DAutomatic handling of concurrent modifications
4.While iterating over a HashMap, a developer uses removeIf on the entry set. What is the outcome?
- AThe map becomes unmodifiable
- BSafe bulk removal of entries
- COnly the first entry is removed
- DConcurrentModificationException is thrown
5.A class implements Iterable but not Iterator. What functionality does this enable?
- AFail-fast behavior during iteration
- BConcurrent modification safety
- CEnhanced for-each loop usage
- DBidirectional traversal of collections
6.What is a key difference between Iterator and ListIterator?
- AIterator can modify collections directly
- BListIterator is fail-fast
- CIterator has more methods than ListIterator
- DListIterator supports bidirectional traversal
7.A developer needs to remove elements from a list during iteration. Which method ensures safety?
- AUsing a for-each loop
- BDirectly accessing list indices
- CList.remove()
- DIterator.remove()
8.What must a class implement to be used in a for-each loop in Java?
- AListIterator interface
- BIterable interface
- CCloneable interface
- DIterator interface