What is the primary advantage of using lambda expressions in Java 8+?
AThey reduce boilerplate code and enable functional programming style
BThey increase execution speed significantly
CThey eliminate the need for classes
DThey are required for all stream operations
Correct Answer:
A. They reduce boilerplate code and enable functional programming style
EXPLANATION
Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.
Consider: List list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
AMap
BMap
CList
DSet
Correct Answer:
A. Map
EXPLANATION
Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.
What is the time complexity of reducing a stream with a lambda expression using reduce()?
AO(n) in sequential, potentially O(log n) in parallel with proper associative operation
BAlways O(1)
CO(n²) regardless of parallelism
DO(n log n) always
Correct Answer:
A. O(n) in sequential, potentially O(log n) in parallel with proper associative operation
EXPLANATION
Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.