1. What is a functional interface in Java?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Runnable and Comparator are both examples of standard functional interfaces in Java.
6. What is the main advantage of using functional interfaces?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
The Consumer functional interface is used for operations that accept a single input argument and return no result, thereby consuming the provided object.