Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 19
Given code of Test.java file:
- package com.udayan.ocp;
- import java.util.Arrays;
- import java.util.List;
- import java.util.function.Predicate;
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = Arrays.asList(-80, 100, -40, 25, 200);
- Predicate<Integer> predicate = num -> {
- int ctr = 1;
- boolean result = num > 0;
- System.out.print(ctr++ + ".");
- return result;
- };
- list.stream().filter(predicate).findFirst();
- }
- }
What will be the result of compiling and executing Test class?
- A 1.2.
- B 1.1.1.1.1.
- C 1.1.
- D 1.2.3.4.5.
- E 1.
- F Nothing is printed on to the console.
Reveal correct answer
Correct answer: C
Explanation
findFirst() is terminal operation.
list.stream() => [-80, 100, -40, 25, 200].
filter(predicate) is executed for each element until just one element passes the test. Because findFirst() will terminate the operation on finding first matching element.
NOTE: a new instance of Predicate is used, hence every time ctr will be initialize to 1.
For -80, Output is '1.' but predicate returns false, hence findFirst() doesn't terminate the operation.
For 100, '1.' is appended to previous output, so on console you will see '1.1.' and predicate returns true, hence findFirst() finds an element and terminates the operation.
Final output is: '1.1.'
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
