Java OCA OCP Practice Question 3051

Question

Choose the correct option based on this code segment:

String []exams = { "OCAJP 8", "OCPJP 8", "Upgrade to OCPJP 8" };
Predicate isOCPExam = exam -> exam.contains("OCP");               // LINE-1
List<String> ocpExams = Arrays.stream(exams)
                              .filter(exam -> exam.contains("OCP"))
                              .collect(Collectors.toList());      // LINE-2
boolean result =
        ocpExams.stream().anyMatch(exam -> exam.contains("OCA")); // LINE-3
System.out.println(result);
  • a) this code results in a compiler error in line marked with the comment LINE-1
  • b) this code results in a compiler error in line marked with the comment LINE-2
  • c) this code results in a compiler error in line marked with the comment LINE-3
  • d) this program prints: true
  • e) this program prints: false


a)

Note

the functional interface Predicate<T> takes type T as the generic parameter that is not specified in LINE-1.

this results in a compiler error because the lambda expression uses the method contains() in the call exam.contains("OCP").

If Predicate<String> were specified, this code segment would compile without errors, and when executed will print "false".




PreviousNext

Related