ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
Stream.of("a", "bb", "ccc")
      .filter(s -> { System.out.print("f" + s.length()); return s.length() > 1; })
      .map(s -> { System.out.print("m"); return s; })
      .findFirst();
  1. A. f1f2f3mm
  2. B. f1f2m
  3. C. f1f2f3m
  4. D. f2m

Correct answer: B. f1f2m

Streams are lazy and process elements one at a time through the whole pipeline. "a" fails the filter, "bb" passes and is mapped, and findFirst short-circuits, so "ccc" is never examined.

More OOP with C++ and Java questions

Exams that ask this topic