Java OCA OCP Practice Question 2121

Question

Choose the correct option based on this program:.

import java.util.stream.DoubleStream;

public class Main {
    public static void main(String []args) {
        DoubleStream nums = DoubleStream.of (1.0, 2.0, 3.0).map(i -> -i); // #1
        System.out .printf("count = %d, sum = %f", nums.count(), nums.sum());
    }
 }
  • a . this program results in a compiler error in the line marked with comment #1
  • B. this program prints: "count = 3, sum = -6.000000"
  • C. this program crashes by throwing a java.util.IllegalFormatConversionException
  • d. this program crashes by throwing a java.lang.IllegalStateException


d.

Note

a stream is considered "consumed" when a terminal operation is called on that stream.

the methods count() and sum() are terminal operations in DoubleStream.

When this code calls nums.count(), the underlying stream is already "consumed".

When the printf calls nums.sum(), this program results in throwing java.lang.IllegalStateException due to the attempt to use a consumed stream.




PreviousNext

Related