Courseiva
Working with Streams and Lambda ExpressionshardMultiple ChoiceObjective-mapped

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

Given: List<String> words = List.of("apple", "banana", "cherry");

var result = words.stream().filter(s -> s.length() > 5).collect(Collectors.toUnmodifiableList());

What happens if another thread tries to add to result?

⚠ Common exam trap

Candidates often confuse `Collectors.toUnmodifiableList()` with `Collectors.toList()`, which returns a mutable list, and incorrectly assume the list can be modified by another thread.

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

UnsupportedOperationException at runtime.

B is correct because `Collectors.toUnmodifiableList()` returns an unmodifiable list, and any attempt to modify it (e.g., via `add()`) throws an `UnsupportedOperationException`. The stream pipeline itself is safe and compiles fine; the issue is the mutability of the resulting collection.

Answer analysis

Option-by-option breakdown

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

  • Compilation error because var cannot be used with streams.

    Why it's wrong here

    var is perfectly valid here; the stream pipeline compiles.

  • UnsupportedOperationException at runtime.

    Why this is correct

    The returned list is unmodifiable; calling add() throws UnsupportedOperationException.

  • The thread successfully adds the element.

    Why it's wrong here

    The list is unmodifiable so add will fail.

  • NullPointerException.

    Why it's wrong here

    NullPointerException would occur only if a null element is added, but here the list is unmodifiable.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.