Java OCA OCP Practice Question 698

Question

How many of these lines fail to compile?

Predicate<String> pred1 = s -> false; 
Predicate<String> pred2 = (s) -> false; 
Predicate<String> pred3 = String s -> false; 
Predicate<String> pred4 = (String s) -> false; 
  • A. One
  • B. Two
  • C. Three
  • D. Four


A.

Note

When creating a lambda with only one parameter, there are a few variants.

The pred1 approach shows the shortest way, where the type is omitted and the parentheses are omitted.

The pred2 approach is similar except it includes the parentheses.

Both are legal.

The pred4 approach is the long way with both the parentheses and type specified.

The only one that doesn't compile is pred3.

The parentheses are required if including the type.




PreviousNext

Related