Java OCA OCP Practice Question 2131

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> notNull =
                ((Predicate<String>)(arg -> arg == null)).negate(); // #1
        System.out.println(notNull.test(null));
    }
}
  • a . this program results in a compiler error in line marked with the comment #1
  • B. this program prints: true
  • C. this program prints: false
  • d. this program crashes by throwing NullPointerException


C.

Note

the expression ((Predicate<String>)(arg -> arg == null)) is a valid cast to the type (Predicate<String>) for the lambda expression (arg -> arg == null).

hence, it does not result in a compiler error.

the negate function in Predicate interface turns true to false and false to true.

hence, given null, the notNull.

test(null) results in returning the value false.




PreviousNext

Related