Java OCA OCP Practice Question 2209

Question

Choose the correct option based on this program:

import java.util.function.Predicate;

public class Main {
   public static void main(String args[]){
        Predicate<String> predContains = "I am going to write OCP8 exam"::contains;
       checkString(predContains, "OCPJP");
   }/*  ww w. ja v  a 2 s  .c om*/
   static void checkString(Predicate<String> predicate, String str) {
       System.out.println(predicate.test(str) ? "contains" : "doesn't contain");
   }
}
  • A. this program results in a compiler error for code within main() method
  • B. this program results in a compiler error for code within checkString() method
  • C. this program prints: contains
  • d. this program prints: doesn't contain


d.

Note

You can create method references for object as well, so the code within main() compiles without errors.

the code within checkString() method is also correct and hence it also compiles without errors.

the string OCPJP is not present in the string "I am going to write OCP8 exam" and hence this program prints "doesn't contain" on the console.




PreviousNext

Related