Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 3
Given code of Test.java file:
- package com.udayan.ocp;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class Test {
- public static void main(String[] args) {
- List<String> list = Arrays.asList("M", "R", "A", "P");
- Collections.sort(list, null);
- list.stream().peek(System.out::print);
- }
- }
What will be the result of compiling and executing Test class?
- A MRAP
- B AMPR
- C RPMA
- D Runtime Exception
- E None of the other options
Reveal correct answer
Correct answer: E
Explanation
Streams are lazily evaluated, which means if terminal operations such as: forEach, count, toArray, reduce, collect, findFirst, findAny, anyMatch, allMatch, sum, min, max, average etc. are not present, the given stream pipeline is not evaluated and hence peek() method doesn't print anything 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.
