ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE

java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo = sq.compose(addOne)…

Function<Integer,Integer> sq = x -> x * x;
Function<Integer,Integer> addOne = x -> x + 1;
Function<Integer,Integer> combo = sq.compose(addOne);
System.out.print(combo.apply(3));
What is printed?
  1. A. 9
  2. B. 16
  3. C. 10
  4. D. 7

Correct answer: B. 16

compose applies its argument function (addOne) first, then the original function (sq): 3+1 = 4, then 4*4 = 16; this is the reverse order from andThen.

More OOP with C++ and Java questions

Exams that ask this topic