Java OCA OCP Practice Question 2217

Question

Choose the best option based on this program:

import java.util.stream.Stream;

public class Main {
    public static void main(String []args) {
        boolean result = Stream.of("do", "re", "mi", "fa", "so", "la", "ti")
                .filter(str -> str.length() > 5)       // #1
                .peek(System.out::println)             // #2
                .allMatch(str -> str.length() > 5);    // #3
        System.out.println(result);
    }//from   ww  w.j a  v a  2  s . c  o  m
}
  • A. this program results in a compiler error in line marked with comment #1
  • B. this program results in a compiler error in line marked with comment #2
  • C. this program results in a compiler error in line marked with comment #3
  • D. this program prints: false
  • e. this program prints the strings "do", "re", "mi", "fa", "so", "la", "ti", and "false" in separate lines
  • F. this program prints: true


F.

Note

the predicate str -> str.length() > 5 returns false for all the elements because the length of each string is 2.

hence, the filter() method results in an empty stream and the peek() method does not print anything.

the allMatch() method returns true for an empty stream and does not evaluate the given predicate.

hence this program prints true.




PreviousNext

Related