List Fail-fast Behavior Quiz
What’s the output of the below Java code?
The correct answer is:
ConcurrentModificationException
Let's break it down:
What’s happening?
`list.stream()`
creates a stream view of the original list. Inside `.map()`
, we're modifying the same list `(list.add("x")
)` while iterating over it.This breaks the fail-fast behavior of the stream — the
ArrayList
internally tracks structural modifications and throws a `ConcurrentModificationException`
if a change is detected during iteration.
If you are interested, here is the logic in the Java SDK
For more quizzes, check out the Pro Quizzes page. We are actively working on adding more interesting quizzes.