Java OCA OCP Practice Question 2627

Question

Which of the following are valid lambda expressions? (Choose all that apply.)

  • A. () -> ""
  • B. x,y -> x+y
  • C. (Shape y) -> return 0;
  • D. (Square c) -> {return;}
  • E. Water w -> 39
  • F. () ->
  • G. (Zoo z, m) -> a


A, D.

Note

The first lambda expression is valid, taking no arguments and returning the empty string, so A is correct.

B is incorrect, as more than one parameter requires parentheses ().

C is incorrect, as brackets {} are required when using return.

D is correct, as the expression takes one Square input and returns void.

E is incorrect, as parentheses are required when using the data type Water.

F is incorrect, as it has no right-side expression.

Finally, G is incorrect, as specifying the data type for one parameter in a lambda expression requires you to specify the data type for all parameters in the expression.

In this case, z has a data type and m does not, therefore the expression is invalid.




PreviousNext

Related