OCA Java SE 8 Method - OCA Mock Question Method 11








Question

Which of the following lambda expressions can fill in the blank? (Choose all that apply)

    
public static void main(){
     List<String> list = new ArrayList<>(); 
     list.removeIf(___________________); 
}
  1. s -> s.isEmpty()
  2. s -> {s.isEmpty()}
  3. s -> {s.isEmpty();}
  4. s -> {return s.isEmpty();}
  5. String s -> s.isEmpty()
  6. (String s) -> s.isEmpty()




Answer



A, D, F.

Note

removeIf() expects a Predicate interface.

A Predicate interface takes a parameter list of one parameter using the specified type.

B and C are incorrect since they do not use the return keyword.

The return is required inside braces for lambda bodies.

E is missing the parentheses around the parameter list.

The parentheses are optional for a single parameter with an inferred type.