ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
System.out.print(Stream.of(1, 2, 3).reduce(10, Integer::sum));
- A. 16
- B. 6
- C. 10
- D. 60
Correct answer: A. 16
reduce(identity, op) starts from the identity and folds every element in: 10 + 1 + 2 + 3. (10 is not a true identity for +, which would matter for parallel streams.)
More OOP with C++ and Java questions
- What is printed? java long n = Stream.of(List.of(1, 2), List.of(2, 3)) .flatMap(List::stream).distinct().count(); System.out.print(n);
- What is printed? java Map<Boolean, List<Integer>> m = Stream.of(2, 4) .collect(Collectors.partitioningBy(x -> x > 5))…
- 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))…