Java SE 8 Programmer II — Question 81
Given the code fragment:
UnaryOperator<Double> 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)) //line n2
.forEach(s -> System.out.print(s + " "));
What is the result?
Answer options
- A. 4000.0
- B. 4000
- 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 code attempts to apply a UnaryOperator<Double> on a stream of Double values, which is not compatible in this context. The other options are incorrect as they suggest valid outcomes or errors that do not occur in the given lines.