Why Does a Second forEach Throw IllegalStateException?
Exhibit
List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
Stream<String> stream = words.stream();
stream.filter(w -> w.startsWith("a"))
.map(String::toUpperCase)
.forEach(System.out::println);
// Output: APPLE
// Then:
stream.forEach(System.out::println); // Throws IllegalStateExceptionRefer to the exhibit. A developer runs the code and gets an IllegalStateException on the second forEach. Which statement explains why?
Quick Answer
An IllegalStateException with the message that the stream has already been operated upon or closed is the answer because Java streams are designed to be traversed exactly once. Once a terminal operation like forEach runs, it consumes the stream's underlying pipeline and marks the stream as closed, so any second attempt to invoke a terminal operation on that same stream reference, even an identical forEach call, is rejected at runtime rather than silently re-executing. This is different from how a List or Collection behaves, where you can iterate repeatedly without consequence; a Stream is a one-shot, lazily evaluated pipeline rather than a reusable data structure. That distinction is exactly what this question is testing: recognizing that intermediate operations like filter or map only build up the pipeline, while a terminal operation both triggers evaluation and exhausts the stream. If you need to process the same data more than once, you must create a new stream from the source, for example by calling stream() again on the underlying collection, rather than trying to reuse the original stream variable. Whenever you see code that calls a terminal operation twice on the same stream reference and the question asks what happens, expect IllegalStateException, and remember the reason is the stream's single-use lifecycle rather than any problem with the underlying data itself.
⚠ Common exam trap
A common misconception in OCP Java exam questions is that streams can be reused like collections, or that the exception is due to resource leaks or modification of the source.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The stream has already been operated upon or closed.
A Stream in Java cannot be reused after a terminal operation has been executed. Once the first forEach terminal operation completes, the stream is consumed and closed. Attempting to call another terminal operation (the second forEach) on the same stream reference throws an IllegalStateException with the message 'stream has already been operated upon or closed'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The filter operation is not lazy.
Why it's wrong here
Filter is lazy, but that is not the cause of the exception.
- ✗
The map operation modifies the source list.
Why it's wrong here
Map is non-interfering and does not modify the source.
- ✗
The stream is not closed after the first terminal operation.
Why it's wrong here
Incorrect; the stream is indeed closed.
- ✓
The stream has already been operated upon or closed.
Why this is correct
Correct. A stream cannot be reused after a terminal operation.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on 1Z0-829
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. What is the result of executing this code?
medium- ✓ A.An exception is thrown at runtime.
- B.bbc
- C.Compilation error
- D.baaabc
Why A: The code attempts to call `findFirst()` on a `Stream<String>` that has already been consumed by a previous terminal operation (`forEach`). In Java, streams are single-use; after a terminal operation is executed, the stream is closed and cannot be reused. Any attempt to perform another terminal operation on the same stream throws an `IllegalStateException` at runtime.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.