Exam 1Z0-809: Java SE 8 Programmer II

Java SE 8 Programmer II Exam Answer: BiFunction Result

Question

Given the code fragment: BiFunction<Integer, Double, Integer> val = (t1, t2) -> t1 + t2; //line n1 System.out.println(val.apply(10, 10.5)); What is the result?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

C.

The code fragment defines a BiFunction object named val which takes in two arguments, an Integer and a Double, and returns an Integer. The lambda expression assigned to val sums the two arguments and returns their integer sum.

sql
BiFunction<Integer, Double, Integer> val = (t1, t2) -> t1 + t2;

When val.apply(10, 10.5) is called, the apply method of the BiFunction object val is invoked with the arguments 10 and 10.5. Since 10.5 is a Double value and not an Integer, it will be automatically unboxed to a double primitive value. The apply method then adds 10 and 10.5 (which is equal to 20.5) and returns the result as an Integer.

kotlin
System.out.println(val.apply(10, 10.5)); // Output: 20

Therefore, the correct answer is (A) 20.