Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 65
Question ID: UKOCP74539
Given code:
- package com.udayankhattry.ocp;
- interface Operation {
- long operate(Integer x, Integer y);
- }
- public class Test {
- public static void main(String[] args) {
- int x = 10;
- int y = 20;
- Operation o1 = /*INSERT*/ -> m * n;
- System.out.println(o1.operate(5, 10));
- }
- }
And options below:
1. (Integer m, int n)
2. (Integer m, var n)
3. (int m, int n)
4. (var m, var n)
5. (Integer m, Integer n)
How many of the above options can be used to replace /*INSERT*/ such that output is 50?
- A One option only
- B Two options only
- C Three options only
- D Four options only
- E All five options
Reveal correct answer
Correct answer: B
Explanation
UKOCP74539:
Let's check all the options one by one:
1. (Integer m, int n)
✗ Compilation error, lambda parameters are mapped to operate(Integer, int) method, but interface Operation has operate(Integer, Integer) method.
2. (Integer m, var n)
✗ Compilation error, local variable type inference and explicitly-typed parameter cannot be mixed.
3. (int m, int n)
✗ Compilation error, lambda parameters are mapped to operate(int, int) method, but interface Operation has operate(Integer, Integer) method.
4. (var m, var n)
✓ Local Variable type inference is allowed with lambda parameters. But, mixing of local variable type inference and explicitly-typed parameter / implicitly-typed parameter is not allowed.
5. (Integer m, Integer n)
✓ Lambda parameters are exactly mapped to operate(Integer, Integer) method. Auto-boxing and auto-unboxing are allowed with lambda body, lambda body expression m * n is successfully evaluated using auto-unboxing and its result of int type can be easily assigned to long [return type of operate(Integer, Integer) method].
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
