Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 95
Question ID: UKOCP66404
Given code:
- package com.udayankhattry.ocp;
- import java.util.Arrays;
- import java.util.stream.Stream;
- public class Test {
- public static void main(String[] args) {
- Stream<Integer> stream = Arrays.asList(1,2,3,4,5).stream();
- System.out.println(stream.sum());
- }
- }
What is the result?
- A 15
- B Runtime Exception
- C Compilation error
Reveal correct answer
Correct answer: C
Explanation
UKOCP66404:
Generic Stream<T> interface has following methods:
Optional<T> min(Comparator<? super T> comparator);
Optional<T> max(Comparator<? super T> comparator);
Primitive Stream interfaces (IntStream, LongStream & DoubleStream) has methods min(), max(), sum(), average() and summaryStatistics().
In this case, as stream is a generic interface, hence stream.sum() causes compilation error.
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.
