Java OCA OCP Practice Question 3021

Question

Given this code segment:

IntFunction<UnaryOperator<Integer>> func = i -> j -> i * j;
// LINE
System.out.println(apply);

Which one of these statements when replaced by the comment marked with LINE will print 200?.

  • a) Integer apply = func.apply(10).apply(20);
  • b) Integer apply = func.apply(10, 20);
  • c) Integer apply = func(10 , 20);
  • d) Integer apply = func(10, 20).apply();


a)
  • the IntFunction<R> takes an argument of type int and returns a value of type R.
  • the UnaryOperator<T> takes an argument of type T and returns a value of type T.

Note

the correct way to invoke func is to call func.apply(10).apply(10) (the other three options do not compile).

the first call apply(10) results in an Integer object that is passed to the lambda expression;

calling apply(20) results in executing the expression (i * j) that evaluates to 200.

the other three options will result in compiler error(s).




PreviousNext

Related