Functional Interfaces — Concept Check
Questions on Functional Interfaces. Master the java.util.function package — Predicate, Function, Consumer, Supplier, and their Bi and primitive variants used throughout the Streams API.
Questions in this quiz
8 questions on Functional Interfaces — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to filter a list of strings to only include those that start with 'A' and are longer than 5 characters. Which functional interface should they use?
- AConsumer<String>
- BSupplier<String>
- CFunction<String, Boolean>
- DPredicate<String>
2.A method reference is used in Java to bridge between OOP methods and functional programming. Which of the following is a valid method reference for a static method?
- AClassName::instanceMethod
- BClassName::staticMethod
- CClassName::new
- Dinstance::instanceMethod
3.In a performance-critical application, a developer uses Predicate<Integer> to check if numbers are even. What is a better alternative?
- ASupplier<Boolean>
- BFunction<Integer, Boolean>
- CBiPredicate<Integer, Integer>
- DIntPredicate
4.A developer wants to create a function that takes two integers and returns their sum. Which functional interface should they use?
- ABiFunction<Integer, Integer, Integer>
- BIntBinaryOperator
- CFunction<Integer, Integer>
- DBinaryOperator<Integer>
5.A service logs messages and also saves them to a file. Which functional interface allows chaining these operations?
- AConsumer<String>
- BSupplier<String>
- CPredicate<String>
- DFunction<String, Void>
6.A developer uses Function<String, Integer> to get the length of a string. What method can they use to chain this with another function that doubles the length?
- AandThen
- Bapply
- Ccompose
- Daccept
7.A developer needs a function that returns a new ArrayList every time it's called. Which functional interface is appropriate?
- ASupplier<List<String>>
- BConsumer<List<String>>
- CPredicate<List<String>>
- DFunction<Void, List<String>>
8.Which of the following functional interfaces extends BiFunction<T, T, T>?
- AFunction<T, T>
- BBiConsumer<T, T>
- CBiPredicate<T, T>
- DBinaryOperator<T>