Return to site
JAVA CERTIFICATION QUESTION: Functional interfaces and lambdas expressions
·
Given:
interface Calculable { int change(int i); default int change(long l){ return (int) l * 3; } } class Test { public static void main(String[] args){ Calculable calculable = l -> l * 2; // Line 1 System.out.println(calculable.change(1)); // Line 2 } }
What is the given program's output?
A. 1
B. 2
C. 3
D. Compilation fails on line 1
E. Compilation fails on line 2
Answer👇👇👇
·ꓭ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ
The Calculable interface has only one abstract method, hence it's a functional interface.
The lambda expression used on line 1 represents that abstract method - it has nothing to do with the default method in the same interface.
That lambda expression doubles its argument.
Therefore, when number 1 is passed to the change method on line 2, number 2 is returned and printed out.