Search Tutorials


Java Functional Interfaces MCQ Questions and Answers | JavaInUse

Java Functional Interfaces MCQ Questions and Answers

Q. What is a functional interface in Java?

A. An interface with only one method
B. An interface that represents a function
C. An interface with abstract methods
D. An interface that can be used as a function

Q. Why are functional interfaces used in Java?

A. To enable lambda expressions
B. To simplify functional programming
C. To provide a way to create anonymous functions
All of the above

Q. Which package contains the functional interfaces in Java?

A. java.util
B. java.lang
C. java.functional
D. java.util.function

Q. How do you identify a functional interface in Java?

A. It must have exactly one abstract method
B. It must extend the FunctionalInterface interface
C. It must have the @FunctionalInterface annotation
All of the above

Q. What is the purpose of the @FunctionalInterface annotation?

A. To indicate that an interface is a functional interface
B. To allow the interface to have multiple abstract methods
C. To enable lambda expressions for the interface
D. To provide additional functionality to the interface

Q. Can a functional interface have multiple abstract methods?

A. Yes, as long as they have default implementations
B. No, it must have exactly one abstract method
C. Yes, but only if they are overloaded methods
D. Yes, but they must have the same method signature

Q. Can a functional interface have default methods?

A. Yes
B. No
C. Only if they are private
D. Only if they are static

Q. Can a functional interface have static methods?

A. Yes
B. No
C. Only if they are private
D. Only if they are final

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. What is the purpose of the Function functional interface in Java 8?

A. To perform an operation on an input value
B. To transform one value into another
C. To filter elements based on a condition
D. To perform a side effect





Q. What is the purpose of the Supplier functional interface in Java 8?

A. To produce a result
B. To consume a value
C. To filter elements
D. To perform an action

Q. What is method referencing in Java 8?

A. Referencing a method using its name
B. Referencing a method using a lambda expression
C. Referencing a method using an anonymous function
D. Referencing a method without specifying its arguments

Q. How do you create a functional interface for a comparator in Java 8?

A. Implement the Comparator interface
B. Extend the Comparator class
C. Use a lambda expression with the compare method
D. Implement the Comparable interface

Q. How do you use a functional interface to perform a mapping operation in Java 8?

A. Implement the Function interface
B. Extend the Function class
C. Use a lambda expression with the apply method
D. Implement the Mapping interface

Q. How do functional interfaces improve code readability in Java 8?

A. By reducing the need for anonymous inner classes
B. By providing a more concise way to express behavior
C. By allowing the use of method references
All of the above

Q. Which of the following functional interfaces from the Java standard library has a method that takes no parameters and returns no value?

A.
@FunctionalInterface
interface MyRunnable {
    void run();
}
B.
@FunctionalInterface
interface MySupplier<T> {
    T get();
}
C.
@FunctionalInterface
interface MyConsumer<T> {
    void accept(T value);
}
D.
@FunctionalInterface
interface MyFunction<T, R> {
    R apply(T value);
}

Q. Which of the following functional interfaces from the Java standard library represents a predicate that can be used to test a condition?

A.
@FunctionalInterface
interface MyPredicate<T> {
    boolean test(T input);
}
B.
@FunctionalInterface
interface MyFunction<T, R> {
    R apply(T value);
}
C.
@FunctionalInterface
interface MySupplier<T> {
    T get();
}
D.
@FunctionalInterface
interface MyConsumer<T> {
    void accept(T value);
}

Q. Which of the following functional interfaces from the Java standard library represents a function that takes one argument and produces a result?

A.
@FunctionalInterface
interface MyFunction<T, R> {
    R apply(T value);
}
B.
@FunctionalInterface
interface MyConsumer<T> {
    void accept(T value);
}
C.
@FunctionalInterface
interface MySupplier<T> {
    T get();
}
D.
@FunctionalInterface
interface MyPredicate<T> {
    boolean test(T input);
}

Q. Which of the following functional interfaces from the Java standard library represents an operation that takes one argument and returns no value?

A.
@FunctionalInterface
interface MyConsumer<T> {
    void accept(T value);
}
B.
@FunctionalInterface
interface MySupplier<T> {
    T get();
}
C.
@FunctionalInterface
interface MyFunction<T, R> {
    R apply(T value);
}
D.
@FunctionalInterface
interface MyPredicate<T> {
    boolean test(T input);
}

Q. Which of the following functional interfaces from the Java standard library represents a supplier of results?

A.
@FunctionalInterface
interface MySupplier<T> {
    T get();
}
B.
@FunctionalInterface
interface MyConsumer<T> {
    void accept(T value);
}
C.
@FunctionalInterface
interface MyFunction<T, R> {
    R apply(T value);
}
D.
@FunctionalInterface
interface MyPredicate<T> {
    boolean test(T input);
}

Q. Which of the following code snippets demonstrates the correct way to create a lambda expression that increments a given integer by 1?

A.
(int x) -> x + 1
B.
(int) -> x + 1
C.
() -> x + 1
D.
x -> x + 1

Q. Which of the following code snippets demonstrates the correct way to use a lambda expression to calculate the square of a given integer?

A.
int result = Integer.valueOf(5).map(x -> x * x);
B.
int result = Integer.map(5, x -> x * x);
C.
int result = map(5, x -> x * x);
D.
int result = 5.map(x -> x * x);

Q. Which of the following code snippets demonstrates the correct way to use a lambda expression to filter a list of integers and keep only the even numbers?

A.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.filter(x -> x % 2 == 0);
B.
List<Integer> numbers = new ArrayList<>();
numbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
numbers.removeIf(x -> x % 2 != 0);
C.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> filtered = new ArrayList<>();
for (int num : numbers) {
    if (num % 2 == 0) {
        filtered.add(num);
    }
}
D.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> filtered = numbers.stream()
    .filter(x -> x % 2 == 0)
    .collect(Collectors.toList());

Q. Which of the following code snippets demonstrates the correct way to use a lambda expression to calculate the sum of a list of integers?

A.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.reduce(0, (x, y) -> x + y);
B.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, Integer::sum);
C.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = 0;
for (int num : numbers) {
    sum += num;
}
D.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().mapToInt(Integer::intValue).sum();

Q. Which of the following code snippets demonstrates the correct way to use a method reference to sort a list of strings in descending order based on their length?

A.
List<String> strings = Arrays.asList("apple", "banana", "cherry");
strings.sort(Comparator.comparing(String::length).reversed());
B.
List<String> strings = Arrays.asList("apple", "banana", "cherry");
strings.sort(Comparator.comparing(String::length).reversedOrder());
C.
List<String> strings = Arrays.asList("apple", "banana", "cherry");
strings.sort(Comparator.comparing(String::length, Comparator.reverseOrder()));
D.
List<String> strings = Arrays.asList("apple", "banana", "cherry");
strings.sort(Comparator.comparing(String::length).reversedOrder());