Java Functional Interfaces Quiz - MCQ - Multiple Choice Questions

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 

B. java.util.function.Consumer 

C. java.util.function.Supplier 

D. java.util.function.Predicate 

E. java.util.function.Function

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.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));
    }
}
A. This program results in a compiler error in the line marked with comment #1 

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. 

The negate function in Predicate interface turns true to false and false to true. Hence, given null, the notNull.test(null) results in returning the value false

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
    }
}
A. This program results in a compiler error in the line marked with #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 interface 

B. 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.Supplier interface

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.Callable

B. java.util.Map

C. java.util.Iterator

D. java.lang.Comparable

Answer

The correct answers are A and D.

Explanation 

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.

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. Java Multithreading Quiz
  11. JDBC Quiz
  12. Java Lambda Expressions Quiz
  13. Java Functional Interfaces Quiz
  14. Java Streams API Quiz
  15. Java Date Time Quiz
  16. Java 8 Quiz

Comments