What is the purpose of a Predicate functional interface in Java?
ATo transform one type of object to another
BTo perform an action without returning a value
CTo test a condition and return a boolean value
DTo supply a value without taking any input
Correct Answer:
C. To test a condition and return a boolean value
EXPLANATION
Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.
Which annotation is used to mark an interface as a functional interface in Java?
A@Function
B@FunctionalInterface
C@Lambda
D@Interface
Correct Answer:
B. @FunctionalInterface
EXPLANATION
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
Which of the following is a valid functional interface that can be used with lambda expressions?
AAn interface with exactly one abstract method
BAn interface with multiple abstract methods
CAn interface with no abstract methods
DAn interface with static methods only
Correct Answer:
A. An interface with exactly one abstract method
EXPLANATION
A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.