Java OCA OCP Practice Question 2133

Question

Choose the correct option based on this program:.

import java.util.function.Function;

public class Main {
    public static void main(String []args) {
        Function<Integer, Integer> negate = (i -> -i), square = (i -> i * i),
                     negateSquare = negate.compose(square);

        System.out .println(negateSquare.apply(10));
    }//from  w  w  w  . j av a2 s . c  o m
}
  • a . this program results in a compiler error
  • B. this program prints: -100
  • C. this program prints: 100
  • d. this program prints: -10
  • e. this program prints: 10


B.

Note

the negate.compose(square) calls square before calling negate.

hence, for the given value 10, square results in 100, and when negated, it becomes -100.




PreviousNext

Related