ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
Map<Integer, Long> m = Stream.of("a", "bb", "cc", "ddd", "e")
    .collect(Collectors.groupingBy(String::length, TreeMap::new, Collectors.counting()));
System.out.print(m);
  1. A. {1=1, 2=2, 3=1}
  2. B. {1=2, 2=2, 3=1}
  3. C. {a=1, bb=2, ddd=3}
  4. D. {2=2, 1=2, 3=1}

Correct answer: B. {1=2, 2=2, 3=1}

groupingBy classifies by length, the TreeMap::new supplier keeps keys sorted, and counting() counts each group: lengths 1 (a, e), 2 (bb, cc), 3 (ddd).

More OOP with C++ and Java questions

Exams that ask this topic