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));
  1. A. 7 8
  2. B. 8 7
  3. C. 8 8
  4. 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

Exams that ask this topic