Java OCA OCP Practice Question 2244

Question

Which can fill in the blank so this code outputs true?

import java.util.function.*; 
import java.util.stream.*; 


public class Main { 
   public static void main(String[] args) { 
      Stream<Boolean> hide = Stream.of(true, false, true); 
      Predicate<Boolean> pred = b -> b; 

      boolean found = hide.filter(pred).___(pred); 
      System.out.println(found); 
   } /*  ww w. j a  v  a 2s.  c  om*/
} 
  • A. Only anyMatch()
  • B. Only allMatch()
  • C. Both anyMatch() and allMatch()
  • D. Only noneMatch()
  • E. The code does not compile with any of these options.


C.

Note

The filter() method passes two of the three elements of the stream through to the terminal operation.

This is redundant since the terminal operation checks the same Predicate.

There are two matches with the same value, so both anyMatch() and allMatch() return true, and Option C is correct.




PreviousNext

Related