Java OCA OCP Practice Question 1695

Question

Fill in the blank so this code prints 8.0.

IntStream stream = IntStream.of(6, 10);
LongStream longs = stream.mapToLong(i -> i);
System.out.println(___);
  • A. longs.average().get()
  • B. longs.average().getAsDouble()
  • C. longs.getAverage().get()
  • D. longs.getAverage().getAsDouble()


B.

Note

Primitive streams, like LongStream, declare an average() method, while summary statistics classes, like LongSummaryStatistics, declare a getAverage() method, making Options C and D incorrect.

The average() method returns an OptionalDouble object, which declares a getAsDouble() method rather than a get() method.

Therefore, Option A is incorrect, and Option B is correct.




PreviousNext

Related