Java SE 8 Programmer II — Question 105
Given the code fragment:
BiFunction<Integer, Double, Integer> val = (t1, t2) -> t1 + t2; //line n1
//line n2
System.out.println(val.apply(10, 10.5));
What is the result?
Answer options
- A. 20
- B. 20.5
- C. A compilation error occurs at line n1.
- D. A compilation error occurs at line n2.
Correct answer: D
Explanation
The correct answer is D because the BiFunction is defined to take an Integer and a Double, but the lambda expression attempts to add them directly, which leads to a type mismatch. Line n1 does not cause a compilation error, and thus the other options are incorrect.