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);- A. {1=1, 2=2, 3=1}
- B. {1=2, 2=2, 3=1}
- C. {a=1, bb=2, ddd=3}
- 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
- 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…
- Which statement about Java anonymous classes is correct?
- Which lines fail to compile? java // Java 10 or later var list = new ArrayList<>(); // L1 list.add(1); list.add("a"); // L2 var x; // L3…