This post contains a few useful Java Functional Interfaces multiple-choice questions to self-test your knowledge on Java 8 Functional Interfaces.
The answer and explanation have been given for each MCQ.
1. Which of the following are functional interfaces? (Select ALL that apply)
A. java.util.stream.Stream
Answer
B, C, D, and E
Explanation
The interface java.util.stream.Stream is not a functional interface–it has numerous abstract methods. The other four options are functional interfaces.
The functional interface java.util.function.Consumer has an abstract method with the signature void accept(T t).
The functional interface java.util.function.Supplier has an abstract method with the signature T get().
The functional interface java.util.function.Supplier has an abstract method with the signature T get().
The functional interface java.util.function.Predicate has an abstract method with the signature boolean test(T t);
The functional interface java.util.function.Function has an abstract method with the signature R apply(T t).
2. Choose the correct option based on this program:
import java.util.function.Predicate;
public class PredicateTest {
public static void main(String[] args) {
Predicate < String > notNull =
((Predicate < String > )(arg - > arg == null)).negate(); // #1
System.out.println(notNull.test(null));
}
}
B. This program prints: true
C. This program prints: false
D. This program crashes by throwing NullPointerException
Answer
C. This program prints: false
Explanation
The expression ((Predicate)(arg -> arg == null)) is a valid cast to the
type (Predicate) for the lambda expression (arg -> arg == null). Hence,
it does not result in a compiler error.
3. Choose the correct option based on this program:
import java.util.function.BiFunction;
public class StringCompare {
public static void main(String args[]) {
BiFunction < String, String, Boolean > compareString = (x, y) - >
x.equals(y);
System.out.println(compareString.apply("Java8", "Java8")); // #1
}
}
B. This program prints: true
C. This program prints: false
D. This program prints: (x, y) -> x.equals(y)
E. This program prints: ("Java8", "Java8") -> "Java8".equals("Java8")
Answer
B. This program prints: true
Explanation
The BiFunction interface takes two type arguments–they are of types String in this
program. The return type is Boolean.
The BiFunction functional interface has an abstract
method named apply(). Since the signature of String’s equals() method matches
that of the abstract method's signature, this program compiles fine. When
executed, the strings “Java8” and “Java8” are equal; hence, the evaluation returns true
that is printed on the console.
4. Which one of the following abstract methods does not take any argument but returns a value?
A. The accept() method in java.util.function.Consumer interfaceB. The get() method in java.util.function.Supplier interface
C. The test() method in java.util.function.Predicate interface
D. The apply() method in java.util.function.Function interface
Answer
B. The get() method in java.util.function.Supplierinterface
Explanation
The signature of get() method in java.util.function.Supplier
interface is: T get().
5. Which of the following interfaces of the Java API can be considered functional?
A. java.util.concurrent.CallableB. java.util.Map
C. java.util.Iterator
D. java.lang.Comparable
Answer
The correct answers are A and D.
Explanation
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.
References
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- Java Multithreading Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz