1Z0-809 · Question #46
Which statement is true about the single abstract method of the java.util.function.Function interface?
The correct answer is D. It accepts an argument and produces a result of any data type. Function<T, R> declares R apply(T t) - two independent type parameters mean the input and output can be completely different types (e.g., Function<String, Integer> parses a String into an Integer). That's why D is correct: no restriction ties the return type to the argument…
Question
Options
- AIt accepts one argument and returns void.
- BIt accepts one argument and returns boolean.
- CIt accepts one argument and always produces a result of the same type as the argument.
- DIt accepts an argument and produces a result of any data type.
How the community answered
(56 responses)- A5% (3)
- B13% (7)
- C4% (2)
- D79% (44)
Explanation
Function<T, R> declares R apply(T t) - two independent type parameters mean the input and output can be completely different types (e.g., Function<String, Integer> parses a String into an Integer). That's why D is correct: no restriction ties the return type to the argument type.
Why the distractors fail:
- A describes
Consumer<T>- same single argument, butaccept()returnsvoid. - B describes
Predicate<T>- same single argument, buttest()returnsboolean. - C is the trickiest distractor:
Functioncan return the same type (e.g.,Function<String, String>), but it is not required to - the type parameters T and R are independent, so this constraint simply doesn't exist.
Memory tip: Map the four core functional interfaces to their signatures - Supplier gives, Consumer takes, Predicate tests (→ boolean), and Function transforms (T → R, anything to anything).
Community Discussion
No community discussion yet for this question.