Java OCA OCP Practice Question 1595

Question

What is the output of the following?

Stream<String> s = Stream.of("Atlanta", "Chicago", "New York");
long count = s.filter(c -> c.startsWith("C")).count();
System.out.print(count);
  • A. 1
  • B. 2
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


A.

Note

In streams, the filter() method filters out any values that do not match.

This means the only value to make it to the terminal operator count() is Chicago, and Option A is correct.




PreviousNext

Related