Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.