Java OCA OCP Practice Question 1645

Question

Which functional interface, when filled into the blank, allows the class to compile?

package mypkg;//from ww  w.j  a v  a2 s.  com
import java.util.function.*;


public class Main {
   public void mine(____ lambda) {
      // TODO: Apply functional interface
   }
   public static void main(String[] debris) {
      new Main().mine((s,p) -> s+p);
   }
}
  • A. BiConsumer<Integer,Double>
  • B. BiFunction<Integer,Double,Double>
  • C. BiFunction<Integer,Integer,Double>
  • D. Function<Integer,Double>


B.

Note

The lambda (s,p) -> s+p takes two arguments and returns a value.

Option A is incorrect because BiConsumer does not return any values.

Option D is also incorrect, since Function only takes one argument and returns a value.

Options B and C both use BiFunction, which takes two generic arguments and returns a generic value.

Option C is incorrect because the datatype of the unboxed sum s+q is int and int cannot be autoboxed or implicitly cast to Double.

Option B is correct.

The sum s+p is of type double, and double can be autoboxed to Double.




PreviousNext

Related