Java OCA OCP Practice Question 2152

Question

Which of the following are valid lambda expressions? (Choose three.).

  • A. () -> {}
  • B. (Double adder) -> {int y; System.out.print(adder); return adder;}
  • C. (Long w) -> {Long w=5; return 5;}
  • D. (int count, vote) -> count*vote
  • E. dog -> dog
  • F. name -> {name.toUpperCase()}


A, B, E.

Note

First off, Option A is a valid functional interface that matches the Runnable functional interface.

Option B is also a valid lambda expression that matches Function<Double,Double>, among other functional interfaces.

Option C is incorrect because the local variable w cannot be declared again in the lambda expression body since it is already declared in the lambda expression.

Option D is also incorrect.

If the data type is specified for one variable in a lambda expression, it must be specified for all variables within the expression.

Next, Option E is correct because this lambda expression matches the UnaryOperator functional interface.

Lastly, Option F is incorrect.

The statement name.toUpperCase() is missing a semicolon ( ;) that is required to terminate the statement.




PreviousNext

Related