ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x * 2; System.out.print(f.andThen(g).apply(3) + " " + f.compose(g).apply(3));
- A. 7 8
- B. 8 7
- C. 8 8
- D. 7 7
Correct answer: B. 8 7
f.andThen(g) applies f first, then g: (3 + 1)·2 = 8. f.compose(g) applies g first, then f: 3·2 + 1 = 7.
More OOP with C++ and Java questions
- What is printed? java List<String> l = new ArrayList<>(List.of("ccc", "ee", "a", "bb", "d"))…
- What is printed? java int[] c = {0}; Runnable r = () -> c[0]++; r.run(); r.run(); System.out.print(c[0]);
- What is printed? java List<Integer> a = new ArrayList<>(List.of(1, 2)); List<Integer> b = new LinkedList<>(List.of(1, 2))…
- What is printed? java Queue<Integer> q = new ArrayDeque<>(); q.add(1); q.add(2); q.offer(3); q.poll(); System.out.print(q.peek() + " " +…
- What is printed? java System.out.print(Stream.of(1, 2, 3).reduce(10, Integer::sum));
- 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);