Java SE 11 Developer — Question 23
var numbers = List.of(0,1,2,3,4,5,6,7,8,9);
You want to calculate the average of numbers.
Which two codes will accomplish this? (Choose two.)
Answer options
- A. double avg = numbers.stream().parallel().averagingDouble(a −> a);
- B. double avg = numbers.parallelStream().mapToInt (m −> m).average().getAsDouble();
- C. double avg = numbers.stream().mapToInt (i −> i).average().parallel();
- D. double avg = numbers.stream().average().getAsDouble();
- E. double avg = numbers.stream().collect(Collectors.averagingDouble(n −> n));
Correct answer: B, D
Explanation
Option B is correct because it uses parallelStream and mapToInt to calculate the average properly. Option D is incorrect because the stream method does not have a direct average() method that returns a double, while the correct way to retrieve the average must involve a call to getAsDouble().