What is the return type of a lambda expression used with IntStream.range(1, 5).map(x -> x * 2)?
Avoid
Bint
CIntStream
DFunction
Correct Answer:
C. IntStream
EXPLANATION
map() is an intermediate operation that returns an IntStream. The lambda expression (x -> x * 2) transforms each int, but map() itself returns IntStream, not the transformed value type.
Correct Answer:
A. Optional.of(value).map(x -> x.toUpperCase()).orElse("default")
EXPLANATION
Optional with map() using a lambda expression is the functional programming approach. It's safe, readable, and chains operations effectively. Options B, C, and D are imperative approaches.
What is the difference between a lambda expression and an anonymous inner class in Java?
ALambda expressions are only for functional interfaces, while anonymous classes can implement any interface
BLambda expressions are cleaner and more concise for functional interfaces
CLambda expressions create new scope, while anonymous classes don't
DBoth A and B
Correct Answer:
D. Both A and B
EXPLANATION
Lambda expressions can only be used with functional interfaces (single abstract method), whereas anonymous classes can implement any interface. Lambda expressions are also more concise.
Both expressions correctly sort by length. Option B uses Integer.compare (safer for int overflow), while Option C uses direct subtraction. Both work correctly here.
What is the correct way to chain lambda expressions using Function interface?
Function f1 = x -> x * 2;
Function f2 = x -> x + 5;
How to apply f1 first, then f2?
Af1.andThen(f2).apply(3)
Bf1.compose(f2).apply(3)
Cf2.apply(f1.apply(3))
DA and C
Correct Answer:
D. A and C
EXPLANATION
Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.
Consider: Stream.of(1, 2, 3).map(x -> { System.out.println(x); return x * 2; }).collect(Collectors.toList()); What will print?
A1 2 3
B2 4 6
CNothing until the collect operation completes
D1 2 3 2 4 6
Correct Answer:
A. 1 2 3
EXPLANATION
map() is an intermediate operation. The println executes because collect() is a terminal operation that triggers evaluation. It prints 1, 2, 3 (the original values).
Consider: Function curried = a -> b -> a + b; What is this pattern called?
AHigher-order function with currying
BNested lambda expression
CFunction composition
DStream reduction
Correct Answer:
A. Higher-order function with currying
EXPLANATION
This is a curried function - a higher-order function that takes one argument and returns another function taking the next argument. It transforms multi-argument functions into sequences of single-argument functions.
What is the time complexity of reducing a stream with a stateful lambda operation?
AO(n) for sequential, O(log n) for parallel
BO(n) for both sequential and parallel streams
CO(1) regardless of stream size
DDepends on the operation and stream characteristics
Correct Answer:
D. Depends on the operation and stream characteristics
EXPLANATION
Time complexity depends on the specific reduction operation, whether the lambda is stateless, and stream characteristics. Generally O(n), but parallelization overhead affects actual performance.