Java 11 Certification Practice Questions (Answers below)
Section: Working with Streams and Lambda expressions
Objective: Implement functional interfaces using lambda expressions, including interfaces from the java.util.function package
Q1. What will be the result?
Function<Integer, Integer> x = a -> a * 5; Function<Integer, Integer> y = a -> a % 5; Function<Integer, Integer> z = x.compose(y); System.out.println(z.apply(12));
A. The code does not compile
B. Prints 0
C. Prints 10
Q1. C is the correct answer.
The x.compose(y) method calls the Function parameter y before the reference
Function variable x. Hence, first 12 % 5 is done which gives 2. Then 2 is multiplied by 5 which gives 10 as the output.
Click here to learn more.