Java OCA OCP Practice Question 1691

Question

What is the result of the following?

IntStream s = IntStream.empty();
System.out.print(s.average().getAsDouble());
  • A. The code prints 0.
  • B. The code prints 0.0.
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


D.

Note

The average() method returns an OptionalDouble.

This interface has a getAsDouble() method rather than a get() method, so the code does compile.

The stream is empty, so the optional is also empty.

When trying to get the value, the code throws a NoSuchElementException, making Option D correct.




PreviousNext

Related