Java OCA OCP Practice Question 1687

Question

Which can fill in the blank to have the code print true?

Stream<Integer> stream = Stream.iterate(1, i -> i+1);
boolean b = stream.___(i -> i > 5);
System.out.println(b);
A.   anyMatch
B.   allMatch
C.   noneMatch
D.   None of the above


A.

Note

This code generates an infinite stream of integers: 1, 2, 3, 4, 5, 6, 7, etc.

The Predicate checks if the element is greater than 5.

With anyMatch(), the stream pipeline ends once element 6 is hit and the code prints true.

For both the allMatch() and noneMatch() operators, they see that the first element in the stream does not match and the code prints false.

Therefore, Option A is correct.




PreviousNext

Related