Java OCA OCP Practice Question 1669

Question

Which lambda expression, when filled into the blank, allows the code to compile?

package mypkg;/*from  www. j a v  a 2s  .co m*/
import java.util.function.*;


public class Main {

   public static Integer rest(BiFunction<Integer,Double,Integer> f) {
      return f.apply(3, 10.2);
   }

   public static void main(String[] participants) {
      rest(___);
   }
}
  • A. (int n, double e) -> (int)(n+e)
  • B. (n,w,e) -> System.out::print
  • C. (s,w) -> 2*w
  • D. (s,e) -> s.intValue()+e.intValue()


D.

Note

While lambda expressions can use primitive types as arguments, the functional interface in this class uses the wrapper classes, which are not compatible.

Option A is incorrect.

Option B is also incorrect, since the number of arguments and return type does not match the functional interface.

The method reference System.out::print on the right-hand side of the lambda expression is invalid here, since it returns a method reference, not a double value.

Option C is incorrect because 2*w is of type double, which cannot be returned as an Integer without an explicit cast.

Option D is the correct answer.

It takes exactly two arguments because the return value int can be implicitly autoboxed to Integer.




PreviousNext

Related