ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Stream<Integer> s = Stream.of(1, 2, 3);
System.out.print(s.count());
System.out.print(s.count());
  1. A. 33
  2. B. 30
  3. C. IllegalStateException before anything is printed
  4. D. 3 is printed, then IllegalStateException is thrown

Correct answer: D. 3 is printed, then IllegalStateException is thrown

A stream can be consumed only once; after a terminal operation it is closed and any further operation throws IllegalStateException.

More OOP with C++ and Java questions

Exams that ask this topic