Java Functional Interfaces MCQ Questions and Answers

1. What is a functional interface in Java?

a) An interface with multiple abstract methods
b) An interface with only one abstract method
c) Any interface in Java
d) An interface that can only be used in functional programming

Answer:

b) An interface with only one abstract method

Explanation:

A functional interface in Java is an interface that contains exactly one abstract method.

2. What annotation is commonly used to denote a functional interface?

a) @Functional
b) @FunctionalInterface
c) @Interface
d) @Lambda

Answer:

b) @FunctionalInterface

Explanation:

The @FunctionalInterface annotation is used to indicate that an interface is intended to be a functional interface.

3. Can functional interfaces have default methods?

a) Yes
b) No
c) Only static methods are allowed
d) Only if they are final

Answer:

a) Yes

Explanation:

Functional interfaces can have any number of default and static methods but only one abstract method.

4. What is the purpose of a functional interface?

a) To create objects
b) To define a single action
c) To group multiple methods
d) To replace classes

Answer:

b) To define a single action

Explanation:

Functional interfaces are used to define a single action, typically to be implemented using a lambda expression or method reference.

5. Which of these is a standard functional interface in Java?

a) Runnable
b) Comparator
c) Both a and b
d) None of the above

Answer:

c) Both a and b

Explanation:

Runnable and Comparator are both examples of standard functional interfaces in Java.

6. What is the main advantage of using functional interfaces?

a) To simplify object creation
b) To facilitate lambda expressions
c) To create multiple methods quickly
d) To support multi-threading

Answer:

b) To facilitate lambda expressions

Explanation:

The main advantage of functional interfaces is that they can be used with lambda expressions for more concise and readable code.

7. Can a functional interface extend another interface?

a) Yes, but it must not add any abstract methods
b) No
c) Only if it's another functional interface
d) Only if it overrides all methods

Answer:

a) Yes, but it must not add any abstract methods

Explanation:

A functional interface can extend another interface, but it must not add more abstract methods to maintain its functional interface status.

8. How can you use method references with functional interfaces?

a) By referring to a method that matches the interface's abstract method
b) By using the 'new' keyword
c) By using the '@' symbol
d) By declaring a method inside the interface

Answer:

a) By referring to a method that matches the interface's abstract method

Explanation:

Method references can be used with functional interfaces by referring to a method that has the same parameters and return type as the abstract method of the functional interface.

9. Can a functional interface have more than one default method?

a) Yes
b) No
c) Only if they are static
d) Only if annotated with @Default

Answer:

a) Yes

Explanation:

A functional interface can have more than one default method, as long as there is only one abstract method.

10. What is the benefit of using the @FunctionalInterface annotation?

a) It enhances performance
b) It ensures the interface meets the requirements of a functional interface
c) It automatically implements the interface
d) It changes the behavior of the interface

Answer:

b) It ensures the interface meets the requirements of a functional interface

Explanation:

The @FunctionalInterface annotation is used for compile-time checking to ensure the interface meets the criteria of having exactly one abstract method.

11. Which functional interface in Java is used for predicates?

a) Function
b) Predicate
c) Supplier
d) Consumer

Answer:

b) Predicate

Explanation:

The Predicate functional interface in Java is used for defining a simple condition or predicate, which returns a boolean value.

12. What functional interface represents a function that accepts a single argument?

a) Runnable
b) BiFunction
c) UnaryOperator
d) Function

Answer:

d) Function

Explanation:

The Function interface represents a function that accepts one argument and produces a result.

13. What is the difference between the Consumer and Supplier interfaces?

a) Consumer accepts an argument and Supplier returns a result
b) Consumer returns a result and Supplier accepts an argument
c) There is no difference
d) Consumer and Supplier are not functional interfaces

Answer:

a) Consumer accepts an argument and Supplier returns a result

Explanation:

The Consumer interface represents an operation that accepts a single input argument and returns no result, while the Supplier interface provides a result without accepting any arguments.

14. Can a lambda expression be used to instantiate a functional interface?

a) Yes
b) No
c) Only if the interface is annotated with @Lambda
d) Only if the interface has no default methods

Answer:

a) Yes

Explanation:

Lambda expressions can be used to provide an implementation for the abstract method of a functional interface, effectively instantiating it.

15. What functional interface is typically used for operations that consume an object but return no result?

a) Function
b) Predicate
c) Supplier
d) Consumer

Answer:

d) Consumer

Explanation:

The Consumer functional interface is used for operations that accept a single input argument and return no result, thereby consuming the provided object.


Comments