Courseiva
Working with Streams and Lambda ExpressionsmediumMultiple ChoiceObjective-mapped

1Z0-829 Working with Streams and Lambda Expressions Practice Question

Exhibit

Refer to the exhibit.
List<String> list = List.of("a", "b", "c");
Stream<String> stream = list.stream();
stream.filter(s -> s.startsWith("b")).forEach(System.out::print);
stream.forEach(System.out::print);

What is the result of executing this code?

⚠ Common exam trap

The trap here is that candidates often forget that streams are single-use and incorrectly assume that chaining multiple terminal operations on the same stream is allowed, leading them to pick a concatenated output like 'baaabc' instead of recognizing the runtime exception.

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

An exception is thrown at runtime.

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.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • An exception is thrown at runtime.

    Why this is correct

    IllegalStateException is thrown when a terminal operation is called on an already-consumed stream.

  • bbc

    Why it's wrong here

    Incorrect output.

  • Compilation error

    Why it's wrong here

    The code compiles; the error occurs at runtime.

  • baaabc

    Why it's wrong here

    This would happen if the stream could be used twice, but it cannot.

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 →

How Courseiva writes practice questions · Editorial policy

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.