Java OCA OCP Practice Question 994

Question

What is the result of the following class?

1: import java.util.function.*; 
2:  //  w  w w.  j  a v  a  2s  .co  m
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 m, Predicate<Main> pred) { 
11:     String result = pred.test(m) ? "match" : "not match";  
12:     System.out.print(result); 
13: } 
14:} 
  • A. match
  • B. not match
  • C. Compiler error on line 8.
  • D. Compiler error on line 10.
  • E. Compiler error on line 11.
  • F. A runtime exception is thrown.


A.

Note

This code is correct.

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

Since there is only one parameter and it does not specify a type, the parentheses around the type parameter are optional.

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




PreviousNext

Related