ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
Map<Boolean, List<Integer>> m = Stream.of(2, 4)
.collect(Collectors.partitioningBy(x -> x > 5));
System.out.print(m.size() + " " + m.get(true));- A. 1 null
- B. 2 []
- C. 1 []
- D. 2 null
Correct answer: B. 2 []
partitioningBy always produces both keys, true and false, even if one partition is empty; here true maps to an empty list. (groupingBy would have only the false key.)
More OOP with C++ and Java questions
- What happens? java interface Show { String toString(); } class C implements Show { } // in main: System.out.print(new C() instanceof Show);
- What is printed? java System.out.print((Double.NaN == Double.NaN) + " " + Double.valueOf(Double.NaN).equals(Double.NaN) + " " + (0.0 ==…
- A class overrides equals correctly, but its hashCode() returns 42 for every object. What happens when many such objects are stored in a…
- What happens? java class P { int v; P(int v) { this.v = v; } } // in main: Set<P> s = new TreeSet<>(); s.add(new P(1)); s.add(new P(2))…
- Which constructors cause compile-time errors? java class F { final int x; F() { } // constructor 1 F(int v) { x = v; x = v + 1; } //…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.compute("a", (k, v) -> v == null ? 1 : v + 1); m.compute("a", (k, v) ->…