Chapter TEN
Java Built-In Lambda Interfaces


Exam Objectives

Use  the built-in interfaces included in the java.util.function package such as Predicate, Consumer, Function, and Supplier.
Develop code that uses primitive versions of functional interfaces.
Develop code that uses binary versions of functional interfaces.
Develop code that uses the UnaryOperator interface.

Answers

1. The correct answer is A.
The and(Predicate) method of the Predicate interface represents a short-circuiting logical AND. This means that the other predicate won't be evaluated if the value of the first predicate can predict the result of the operation (if the first predicate return false in the case of AND).

In the code, when p1 is executed, it prints "p1" and then false is returned, which stops the execution of the second predicate.


2. The correct answer is B.
BiConsumer represents a consumer that takes two arguments and doesn't return a result. This interface takes one object and a primitive value as a second argument, so the naming convention, in this case, is ObjXXXConsumer, where XXX is the primitive type.


3. The correct answer is C.
The compose method (for Functions and UnaryOperators) first applies the operator passed as a parameter to its input and then applies the other operator to the result.

In this case, u2 is executed first, and its result (24) is passed to u1, resulting in 4.


4. The correct answer is D.
Option A is false. A Consumer is an operation that accepts a single input argument and returns no result.
Option B is false. UnaryOperator is a specialization of the Function interface.
Option C is false. The BiFunction interface has primitive versions for int, long, and boolean.
Option D is true. A Supplier represents an operation that takes no arguments, but it returns some value.


5. The correct answer is D.
getAsBoolean() is a method of BooleanSupplier, not Supplier. To compile this program, you either change s to BooleanSupplier or leave it as a Supplier and call the method get().


6. The correct answer is D.
BiPredicate doesn't have primitive versions.


7. The correct answer is B.
Function has three types of primitive versions:


8. The correct answers are A and C.
Option A is true. The BinaryOperator interface extends from the BiFunction interface.
Option B is false. There's no BiSupplier interface. Since Supplier just returns values without taking arguments, it doesn't have any sense to have a binary version.
Option C is true. The Supplier interface doesn't define any default methods.
Option D is false. minBy and maxBy are two static (not default) methods of the BinaryOperator interface.