1Z0-829 Working with Streams and Lambda Expressions Practice Question
Which THREE of the following are true about the Optional class? (Choose three.)
⚠ Common exam trap
Oracle OCP Java 17 often tests the distinction between `Optional.of()` and `Optional.ofNullable()`, and the fact that `Optional.get()` throws `NoSuchElementException` rather than returning null, to catch candidates who confuse Optional with a simple null wrapper.
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
✓
Optional.empty().orElseThrow(IllegalStateException::new) throws IllegalStateException
`Optional.empty().orElseThrow(IllegalStateException::new)` explicitly throws a new `IllegalStateException` when the Optional is empty. The `orElseThrow` method is designed to throw the provided exception supplier's exception if no value is present.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Optional.empty().orElseThrow(IllegalStateException::new) throws IllegalStateException
Why this is correct
orElseThrow with a Supplier will throw the exception if the Optional is empty.
- ✗
Optional.of(null) returns an empty Optional
Why it's wrong here
Optional.of(null) throws NullPointerException; use ofNullable for null-safe.
- ✓
Optional.ofNullable(null) returns an empty Optional
Why this is correct
ofNullable returns Empty Optional if argument is null.
- ✗
Optional.get() on empty Optional returns null
Why it's wrong here
get() throws NoSuchElementException if Optional is empty.
- ✓
Optional.ifPresent(v -> System.out.println(v)) with empty Optional does nothing
Why this is correct
ifPresent only executes the consumer if a value is present.
Go deeper
Related to this question
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 →
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.