Java SE 8 Programmer II — Question 34

Given the code fragment:
UnaryOperator<Integer> uo1 = s -> s*2; line n1
List<Double> loanValues = Arrays.asList(1000.0, 2000.0);
loanValues.stream()
.filter(lv -> lv >= 1500)
.map(lv -> uo1.apply(lv))
.forEach(s -> System.out.print(s + " "));
What is the result?

Answer options

Correct answer: D

Explanation

The correct answer is D because the method apply of UnaryOperator<Integer> expects an Integer argument, but the stream is processing Double values. This type mismatch causes a compilation error at line n2. Options A and B are incorrect as they imply a successful execution of the code, which is not the case, and option C is wrong because line n1 is valid.