Courseiva
Working with Streams and Lambda ExpressionseasyMultiple ChoiceObjective-mapped

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

A method returns an Optional<String>. The developer wants to transform the value inside the Optional to uppercase and print it, but only if present. Which best-practice approach uses streams?

⚠ Common exam trap

The trap here is that candidates may overlook the 'uses streams' constraint and pick A or B, which are valid Optional operations but not stream-based, or pick D which misuses Stream.of to wrap the Optional instead of converting it.

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

opt.stream().map(String::toUpperCase).forEach(System.out::println);

It converts the Optional to a Stream using opt.stream(), then applies map(String::toUpperCase) to transform the value, and finally uses forEach(System.out::println) to print it only if present. This approach leverages the Stream API's lazy evaluation and functional pipeline, which is the best practice for integrating Optional with streams.

Answer analysis

Option-by-option breakdown

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

  • opt.map(String::toUpperCase).ifPresent(System.out::println);

    Why it's wrong here

    This is also valid and uses Optional's map, but it is not a stream approach. The question specifically asks for streams.

  • opt.ifPresent(s -> System.out.println(s.toUpperCase()));

    Why it's wrong here

    This does not use streams; it's a valid approach but not stream-based.

  • opt.stream().map(String::toUpperCase).forEach(System.out::println);

    Why this is correct

    This converts Optional to a stream of one or zero elements, applies transformation, and prints. It is a stream-based best practice.

  • Stream.of(opt).map(o -> o.get().toUpperCase()).forEach(System.out::println);

    Why it's wrong here

    This may throw NoSuchElementException if Optional is empty; also it wraps the Optional itself, not the content.

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.