Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 17
Question ID: UKOCP69133
Given code:
- package com.udayankhattry.ocp;
- import java.util.Optional;
- import java.util.stream.Stream;
- public class Test {
- public static void main(String[] args) {
- Stream<Number> stream = Stream.of();
- Optional<Number> optional = stream.findFirst();
- System.out.println(optional.orElse(-1));
- }
- }
What is the result?
- A null
- B 0
- C -1
- D Optional.empty
Reveal correct answer
Correct answer: C
Explanation
UKOCP69133:
Stream.of() creates an empty stream.
stream.findFirst(); => returns an empty Optional. Hence, orElse method is executed and prints -1 on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
