1Z0-809 · Question #97
Given the code fragment: UnaryOperator<Double> uol = s -> s*2; //line n1 List<Double> loanValues = Arrays.asList(1000.0, 2000.0); loanValues.stream() .filter(lv -> lv >= 1500) .map(lv ->…
The correct answer is A. 4000.0. Option A (4000.0) is correct because the stream filters [1000.0, 2000.0] down to only 2000.0 (the single value satisfying >= 1500), then maps it through uol.apply(2000.0) which computes 2000.0 2 = 4000.0, and forEach prints it with a trailing space - but since…
Question
Options
- A4000.0
- B4000
- CA compilation error occurs at line n1.
- DA compilation error occurs at line n2.
How the community answered
(33 responses)- A76% (25)
- B3% (1)
- C12% (4)
- D9% (3)
Explanation
Option A (4000.0) is correct because the stream filters [1000.0, 2000.0] down to only 2000.0 (the single value satisfying >= 1500), then maps it through uol.apply(2000.0) which computes 2000.0 * 2 = 4000.0, and forEach prints it with a trailing space - but since Double.toString(4000.0) produces "4000.0", the decimal is included in the output.
B is wrong because Java prints double/Double values with a decimal point (e.g., 4000.0, not 4000); integer-looking output would require an explicit cast or integer type.
C is wrong because line n1 is perfectly valid - UnaryOperator<Double> expects a lambda (Double) -> Double, and s -> s * 2 compiles correctly with s inferred as Double.
D is wrong because line n2 is also valid - UnaryOperator<T> extends Function<T, T>, so uol.apply(lv) satisfies Stream.map()'s requirement of a Function<Double, R>.
Memory tip: When you see a stream chain, trace the data one step at a time - filter first (what survives?), then transform (what does each element become?). Also remember: Java always prints floating-point types with a decimal, so any answer showing an integer output (4000) for a Double value is a red flag.
Community Discussion
No community discussion yet for this question.