OCA Java SE 8 Method - OCA Mock Question Method 22








Question

What is the result of the following class?

     1: import java.util.function.*; 
     2:  
     3: public class Main { 
     4:    int age; 
     5:    public static void main(String[] args) { 
     6:       Main p1 = new Main(); 
     7:       p1.age = 1; 
     8:       check(p1, p -> p.age < 5); 
     9:    } 
     10:   private static void check(Main main, Predicate<Main> pred) { 
     11:      String result = pred.test(main) ? "match" : "not match";  
     12:      System.out.print(result); 
     13:   } 
     14:} 
  1. match
  2. not match
  3. Compiler error on line 8.
  4. Compiler error on line 10.
  5. Compiler error on line 11.
  6. A runtime exception is thrown.




Answer



A.

Note

Line 8 creates a lambda expression that checks if the age is less than 5.

Since there is only one parameter without a type, the parentheses around the type parameter are optional.

Line 10 uses the Predicate interface, which declares a test() method.