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();- A. f1f2f3mm
- B. f1f2m
- C. f1f2f3m
- 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
- What is printed? java Stream.of(1, 2, 3).peek(System.out::print).map(x -> x 2); System.out.print("end");
- What is printed? java Map<Integer, Long> m = Stream.of("a", "bb", "cc", "ddd", "e") .collect(Collectors.groupingBy(String::length…
- What is printed? java Map<Integer, String> m = Stream.of("ab", "cd", "e") .collect(Collectors.toMap(String::length, s -> s))…
- Why is the comparator (a, b) -> a - b unsafe for sorting arbitrary int values in Java?
- What is printed? java String s = "ab"; StringBuilder sb = new StringBuilder("ab"); System.out.print(s.equals(sb) + " " +…
- What happens when this class is compiled? java class Code implements Comparable<String>, Comparable<Integer> { public int compareTo(String…