Java OCA OCP Practice Question 1605

Question

How many lines does this code output?

List<String> list = new LinkedList<>();
list.add("A");
list.add("B");


Stream<String> s = list.stream();
s.forEach(System.out::println);
s.forEach(System.out::println);
  • A. Two
  • B. Four
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


D.

Note

Java only allows you to operate on a stream once.

The final line of code throws an IllegalStateException because the stream has already been used up.

Option D is the correct answer.




PreviousNext

Related