exam questions

Exam 1z0-829 All Questions

View all questions & answers for the 1z0-829 exam

Exam 1z0-829 topic 1 question 22 discussion

Actual exam question from Oracle's 1z0-829
Question #: 22
Topic #: 1
[All 1z0-829 Questions]

Given the code fragment:
List<Integer> listOfNumbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Which code fragment returns different values?

  • A. int sum = listOfNumbers.stream().reduce(0, Integer::sum) + 5;
  • B. int sum = listOfNumbers.parallelStream().reduce(0, Integer::sum) + 5 ;
  • C. int sum = listOfNumbers.parallelStream().reduce((m, n) -> m + n).orElse(5) + 5;
  • D. int sum = listOfNumbers.parallelStream().reduce(5, Integer::sum);
  • E. int sum = listOfNumbers.stream().reduce(5, (a, b) -> a+ b);
Show Suggested Answer Hide Answer
Suggested Answer: D 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
SrinivasJasti
2 weeks, 3 days ago
Selected Answer: D
Non-determinism in parallel streams, the elements are partitioned into smaller sub-streams, and the reduction is performed on each sub-stream independently. it's possible that the 5 (initial value provided to reduce()) is added to some elements before they are added to others.
upvoted 1 times
...
xplorerpj
6 months, 3 weeks ago
D is correct answer
upvoted 1 times
...
Samps
8 months ago
// D. We need to be careful while using parallelStream()
upvoted 1 times
...
c6437d5
9 months, 3 weeks ago
Selected Answer: D
D tested correct
upvoted 2 times
...
james2033
10 months, 4 weeks ago
Selected Answer: D
// 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.
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago