OCA Java SE 8 Mock Exam - OCA Mock Question 17








Question

Which of the following lines can be inserted at line 2 to print true? (Choose all that apply)

     public class Main{
         1: public static void main(String[] args) { 
         2:   // INSERT CODE HERE 
         3: } 
         4: private static boolean test(Predicate<Integer> p) { 
         5:   return p.test(2); 
         6: } 
     }
     
  1. System.out.println(test(i -> i == 2));
  2. System.out.println(test(i -> {i == 2;}));
  3. System.out.println(test((i) -> i == 2));
  4. System.out.println(test((int i) -> i == 2);
  5. System.out.println(test((int i) -> {return i == 2;}));
  6. System.out.println(test((i) -> {return i == 2;}));




Answer



A, C, F.

Note

Lambda expressions with one parameter can omit the parentheses around the parameter list, A and C are correct.

The return statement is optional when a single statement is in the body, therefor F is correct.

B is incorrect because a return statement must be used if braces are included around the body.

D and E are incorrect because the type is Integer in the predicate and int in the lambda.