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?
- A. 9
- B. 16
- C. 10
- 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
- java class Base { int v = 1; } class Derived extends Base { int v = 2; } // main: Base b = new Derived(); System.out.print(b.v); What is…
- In C++, a base class constructor calls a function that is overridden in a derived class. Which version actually runs while the base…
- A PaymentMethod type requires every concrete implementation to provide charge() and refund(), but there is no shared implementation at all…
- Which kind of call in C++ is resolved using static (compile-time) binding rather than dynamic (run-time) binding?
- Which of these can a Java interface still NOT have, even in a modern version such as Java 17?
- A class overrides equals() but does not override hashCode(). What bug does this risk for instances used as keys in a HashMap or stored in…