Given the code fragment:
Which should be inserted into line n1 to print Average = 2.5?
Given the code fragment:
Which should be inserted into line n1 to print Average = 2.5?
To print the average of the numbers 1, 2, 3, and 4, which equals 2.5, the correct stream type used should be an IntStream. The method average() is available in IntStream and it returns an OptionalDouble, from which the getAsDouble() method can be called to get the actual double value. The appropriate declaration to achieve this is 'IntStream str = IntStream.of(1, 2, 3, 4);'. Using DoubleStream or Stream would not work correctly with the available methods.
The answer is B.
answer is B-tested
Answer is C. Stream.of() gives Stream<Integer> or Stream<Double>, not IntStream or DoubleStream. So A and C are incorrect. And Stream hat no method average(). So D is incorrect too.
B is the answer
B is correct.
The answer is letter b, it cannot be C using the method in the second line "getAsDouble()".