1Z0-829 Stream.iterate Practice Question
Exhibit
Refer to the exhibit. List<Integer> nums = List.of(1, 2, 3, 4, 5); Optional<Integer> opt = nums.stream().filter(n -> n % 2 == 0).findFirst(); System.out.println(opt.orElse(-1));
What is the output?
⚠ Common exam trap
Candidates often overlook that the iterate function can be subtraction, leading to negative numbers. They may misapply the limit or filter, or forget that reduce uses an identity value. The key is to trace each step carefully and realize that -8 is the actual output, which is not listed.
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
✓
2
The code uses `Stream.iterate(0, n -> n - 1)` to generate an infinite stream starting from 0, decrementing by 1 each step. `limit(5)` restricts to the first five elements: 0, -1, -2, -3, -4. The `filter(n -> n % 2 != 0)` retains only odd numbers: -1 and -3. Then `map(n -> n * 2)` doubles them to -2 and -6. Finally, `reduce(0, Integer::sum)` sums these with identity 0, yielding -8. Since -8 is not among the provided options (1, 2, 0, -1), none of the options is correct.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
1
Why it's wrong here
1, which would result from a different stream processing (e.g., if limit were 1 and map doubled 0.5, but here the output is -8).
- ✓
2
Why this is correct
2, which would result from the commonly assumed iterate(n -> n+1) with limit(2), but the actual code uses decrement, yielding -8.
- ✗
0
Why it's wrong here
0, which would occur if the filter removed all elements or if the identity were the only value, but here we have -1 and -3.
- ✗
-1
Why it's wrong here
1, which is close but incorrect; the sum of doubled odd numbers from the first five decremented integers is -8.
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.