Java OCA OCP Practice Question 1657

Question

Which lambda expression can be passed to the magic() method?

package show;
import java.util.function.*;


public class Main {
   public void magic(BinaryOperator<Long> lambda) {
      lambda.apply(3L, 7L);
   }
}
  • A. magic((a) -> a)
  • B. magic((b,w) -> (long)w.intValue())
  • C. magic((c,m) -> {long c=4; return c+m;})
  • D. magic((Integer d, Integer r) -> (Long)r+d)


B.

Note

BinaryOperator<Long> takes two Long arguments and returns a Long value.

For this reason, Option A, which takes one argument, and Option D, which takes two Integer values that do not inherit from Long, are both incorrect.

Option C is incorrect because the local variable c is redeclared inside the lambda expression, causing the expression to fail to compile.

The correct answer is Option B because intValue() can be called on a Long object.

The result can then be cast to long, which is autoboxed to Long.




PreviousNext

Related