Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy
Question 40
Given code of Test.java file:
- package com.udayan.ocp;
- import java.util.function.BiFunction;
- public class Test {
- public static void main(String[] args) {
- BiFunction<Integer, Integer, Character> compFunc = (i, j) -> i + j;
- System.out.println(compFunc.apply(0, 65));
- }
- }
NOTE: ASCII value of A is 65.
What will be the result of compiling and executing Test class?
- A 65
- B Compilation error
- C A
Reveal correct answer
Correct answer: B
Explanation
BiFunction<T, U, R> : R apply(T t, U u);
BiFunction interface accepts 3 type parameters, first 2 parameters (T,U) are passed to apply method and 3rd type parameter is the return type of apply method.
In this case, 'BiFunction<Integer, Integer, Character>' means apply method will have declaration: 'Character apply(Integer d1, Integer d2)'.
Lambda expression should accept 2 Integer type parameters and must return Character object.
Lambda expression is:
'(i, j) -> i + j', i + j returns an int type and int cannot be implicitly casted to Character and this causes compilation error.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
