Courseiva
Working with Streams and Lambda ExpressionsmediumMultiple ChoiceObjective-mapped

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

Exhibit

Refer to the exhibit.
```java
List<Optional<String>> list = Arrays.asList(
    Optional.of("A"),
    Optional.empty(),
    Optional.of("B"));
List<String> result = list.stream()
    .flatMap(Optional::stream)
    .collect(Collectors.toList());
System.out.println(result);
```

What is the output?

⚠ Common exam trap

A common mix-up: candidates confuse `map` with `flatMap` on optionals, expecting `map` to produce a stream of optionals, but `flatMap` is required to unwrap and flatten the optionals into a stream of their contained values.

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

[A, B]

The code uses `flatMap` on a stream of `Optional<String>` values. `flatMap` flattens nested optionals, so `Optional.of("A")` yields a stream containing "A", `Optional.empty()` yields an empty stream (effectively skipping it), and `Optional.of("B")` yields a stream containing "B". The terminal operation `collect(Collectors.toList())` collects the non-empty strings into a list, resulting in `[A, B]`.

Answer analysis

Option-by-option breakdown

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

  • [A, B]

    Why this is correct

    Correct: Only non-empty Optionals are flattened.

  • NullPointerException

    Why it's wrong here

    Incorrect: No null values are present.

  • [Optional[A], Optional.empty, Optional[B]]

    Why it's wrong here

    Incorrect: flatMap flattens out the Optional wrapper.

  • []

    Why it's wrong here

    Incorrect: There are non-empty Optionals.

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.