Chapter EIGHT
Functional Interfaces


Exam Objectives

Create and use Lambda expressions.

Answers

1. The correct answer is A.
A functional interface must declare one abstract method. Since interface B only declares a static method and inherits a default one, it won't compile, not even by adding another default method or by having the @FunctionalInterface annotation.


2. The correct answers are B and C.
The code doesn't compile successfully. This is not a functional interface because it has two abstract methods (it doesn't matter they have the same name).

The @FunctionalInterface annotation throws an error if the interface it marks is not a functional one. If we remove it, the code compiles although, the interface is still not functional.

If we remove one method, no matter which, the interface becomes functional, making the code compile.

The @FunctionalInterface annotation doesn't make an interface functional; its job is just to make the compiler check if the interface is a functional one.


3. The correct answers are A and C.
There's nothing wrong with the code; it compiles successfully.

Functional interfaces can have any number of default methods, as long as they have only one abstract method. In this case, the code represents a functional interface. If we remove the sum method, we would be violating this rule and the interface wouldn't be functional anymore.


4. The correct answers are A and D.
java.util.concurrent.Callable is marked with the @FunctionalInterface annotation.

Although java.lang.Comparable is not marked with the @FunctionalInterface annotation, it can be considered one. Remember, this annotation doesn't make an interface functional.


5. The correct answer is B.
There's nothing wrong with this code; it compiles successfully. However, this is not a functional interface since it doesn't have an abstract method. Therefore, all other options are false.