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.andThen(addOne)…

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

Correct answer: B. 10

andThen applies the original function (sq) first, then feeds its result into the argument function (addOne): 3*3 = 9, then 9+1 = 10.

More OOP with C++ and Java questions

Exams that ask this topic