Question 1
What does this program print? ```java import java.util.stream.*; public class Main { public static void main(String[] args) { System.out.println(IntStream.of(2, 3, 4, 6).average().getAsDouble()); } } ```
A. OptionalDouble[3.75]
What would print if the getAsDouble() call were dropped and the wrapper passed to println: it is OptionalDouble's toString. getAsDouble() returns the primitive, so no wrapper text appears.
B. 3
Assumes average() performs integer division (15 / 4 = 3), by analogy with sum(). The return type OptionalDouble shows the mean is computed in double, with no truncation.
C. 3.0
The same integer-division belief with the truncated result widened to double on the way out. average() never truncates; the true mean is 3.75.
D. 3.75Correct answer
IntStream.average() computes the mean in double (15 over 4 = 3.75) and returns OptionalDouble; getAsDouble() unwraps the present value to the primitive 3.75.
Explanation
Trace: `IntStream.average()` is declared to return `OptionalDouble` — the arithmetic mean is computed in `double`, not in `int`, even though the source is a primitive int stream. The elements sum to 15 over 4 elements, so the mean is 3.75. `getAsDouble()` unwraps the present `OptionalDouble` to the primitive `double` 3.75, and `println(double)` prints `3.75`. Why the others are wrong: `3` assumes `average()` on an `IntStream` performs integer division (15 / 4 = 3), by analogy with `sum()`, which really does return `int`. The return type `OptionalDouble` is the giveaway that no truncation happens. `3.0` is the same integer-division belief with the truncated result widened to `double` on the way out. `average()` never truncates. `OptionalDouble[3.75]` is what would print if the `getAsDouble()` call were dropped and the wrapper were passed to `println` — it is `OptionalDouble`'s `toString`. The value is right, but `getAsDouble()` returns the primitive, so no wrapper text is printed. Exam tip: on the primitive streams, `sum()` returns the primitive type (`int`/`long`/`double`) and can overflow, while `average()` always returns `OptionalDouble` because an empty stream has no mean. The reverse trap is calling `getAsDouble()` on the average of an empty stream — that throws `NoSuchElementException`.