Reduce Operation in Java Streams — Identity and Optional
A developer writes:
List<Integer> list = List.of(1, 2, 3); Optional<Integer> opt = list.stream().reduce((a, b) -> a + b); System.out.println(opt.get());
What is the result?
Quick Answer
The answer is 6. This is correct because the single-argument `reduce` operation in Java streams applies a `BinaryOperator` to accumulate the stream elements sequentially, starting with the first two elements: here, 1 + 2 = 3, then 3 + 3 = 6. Unlike the three-argument `reduce` with an identity value, this overload returns an `Optional<Integer>` to safely handle empty streams, and calling `get()` on a non-empty `Optional` extracts the result. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this question tests your understanding of the `reduce` method’s return type and its behavior with non-empty versus empty streams—a common trap is forgetting that `get()` on an empty `Optional` throws `NoSuchElementException`. To reduce errors, remember: no identity means an `Optional` is returned, so always check `isPresent()` or use `orElse()` in production code. Memory tip: “One arg, Optional bag; three args, identity tag.”
⚠ Common exam trap
The trap here is that candidates may forget that `reduce` with one argument returns an `Optional` and incorrectly assume the output is `Optional[6]` or that `get()` throws an exception, overlooking that the stream is non-empty.
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
✓
6
The `reduce` method with a single argument (a `BinaryOperator`) returns an `Optional` because the stream might be empty. Here, the stream contains three integers, so the reduction computes `1 + 2 = 3`, then `3 + 3 = 6`, yielding `Optional[6]`. Calling `get()` on a non-empty `Optional` returns the contained value `6`, which is printed.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
6
Why this is correct
Correct output.
- ✗
Optional[6]
Why it's wrong here
Println would print Optional[6] only if opt is printed directly, but opt.get() returns Integer.
- ✗
Optional.empty
Why it's wrong here
Stream is non-empty.
- ✗
NoSuchElementException
Why it's wrong here
Stream has elements.
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way this is tested on 1Z0-829
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Refer to the exhibit. What is the output? ```java import java.util.stream.IntStream; public class Example { public static void main(String[] args) { int sum = IntStream.rangeClosed(0, 5) .filter(i -> i >= 0) .sum(); System.out.println(sum); } } ```
medium- A.0
- ✓ B.15
- C.10
- D.5
Why B: The stream pipeline starts with `IntStream.rangeClosed(0, 5)` producing the integers 0, 1, 2, 3, 4, 5. The filter passes all these values, and the map is an identity function (or absent). The reduction sums them: 0+1+2+3+4+5 = 15. Therefore, the output is 15.
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.