1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 22


Given the code fragment:

List listOfNumbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Which code fragment returns different values?

Show Answer
Correct Answer: D

The code fragment that returns different values is: int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum);. This is because the reduce operation with a parallel stream uses the given identity value (5 in this case) as the initial value for each partition of the stream. Consequently, each element of the list will be summed with 5 independently in parallel, resulting in a different final total compared to using a standard stream or other parallel stream reductions with different identities or reduction methods.

Discussion

4 comments
Sign in to comment
c6437d5Option: D
Mar 25, 2024

D tested correct

james2033Option: D
Feb 22, 2024

// D. int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum); // (1 + 5) + (2 + 5) + (3 + 5) + (4 + 5) + (5 + 5) + (6 + 5) + (7 + 5) + (8 + 5) + (9 + 5) + (10 + 5) = 105 System.out.println(">>> sum = " + sum); // >>> sum = 105 A, B, C, E return sum = 60.

SampsOption: D
May 15, 2024

// D. We need to be careful while using parallelStream()

xplorerpjOption: D
Jun 25, 2024

D is correct answer