Courseiva
Working with Streams and Lambda ExpressionsmediumMultiple ChoiceObjective-mapped

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

A developer wants to collect elements from a stream into an immutable List. Which collector should be used?

⚠ Common exam trap

Test-takers frequently think `Collectors.toList()` returns an immutable list (since it's often used in read-only contexts), or they may overcomplicate the solution by chaining collectors when a direct method exists.

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

Collectors.toUnmodifiableList()

`Collectors.toUnmodifiableList()` directly returns a collector that accumulates elements into an immutable `List`. This was introduced in Java 10 and guarantees that the resulting list cannot be modified, throwing `UnsupportedOperationException` on any mutation attempt.

Answer analysis

Option-by-option breakdown

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

  • Collectors.toList()

    Why it's wrong here

    Returns a mutable list.

  • Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)

    Why it's wrong here

    This works but is not the direct collector. The question asks 'which collector', so the direct one is toUnmodifiableList.

  • Collectors.toList().stream().collect(Collectors.toUnmodifiableList())

    Why it's wrong here

    This is inefficient and not a single collector.

  • Collectors.toUnmodifiableList()

    Why this is correct

    Correct: Returns an unmodifiable list.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.