What does the Java expression Stream.of(1, 2, 3, 4).filter(n -> n % 2 == 0).map(n -> n * n).reduce(0, Integer::sum) evaluate to?
Correct answer B. 20
Explanation
The filter keeps only the even numbers 2 and 4, map squares them to 4 and 16, and reduce adds them to the starting value 0, giving 20. The value 30 forgets the filter and sums every square, while 6 sums the evens without squaring. Operations apply in the order written.
Answer checked How we write and check questions
Discussion