What will be the output of this nested lambda?
Function<Integer, Function<Integer, Integer>> add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
Answer: A
Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.
Q.2Hard
What will happen if you try to compile this code?
BiFunction<String, String, String> concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
Answer: C
BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.
Q.3Hard
What is the type inference mechanism in lambda expressions?
Answer: A
Compiler uses target type (functional interface) to infer parameter types. If context is clear, explicit types not needed: list.forEach(x -> ...).
Q.4Hard
Consider a lambda that needs to throw a checked exception. Which approach is correct?
Answer: A
Lambda expressions don't propagate checked exceptions. They must be caught within the lambda body using try-catch.
Q.5Hard
What is the time complexity of reducing a stream with a lambda expression using reduce()?
Answer: A
Sequential reduce is O(n). Parallel reduction can be O(log n) with a properly associative and commutative operation, enabling divide-and-conquer approach.
Advertisement
Q.6Hard
Consider: List<String> list = Arrays.asList("a", "b", "c"); list.stream().collect(Collectors.toMap(s -> s, s -> s.length())). What type is returned?
Answer: A
Collectors.toMap with key mapper (s -> s) produces String keys and value mapper (s -> s.length()) produces Integer values, resulting in Map<String, Integer>.
Q.7Hard
What issue can arise when using lambda expressions with flatMap() in nested streams?
Answer: A
Each flatMap creates intermediate streams. Deeply nested flatMap with lambdas can consume significant memory, especially with large datasets.
Q.8Hard
Consider: Function<Integer, Function<Integer, Integer>> curry = x -> y -> x + y; What does curry.apply(5).apply(3) return?
Answer: A
This is a curried function. curry.apply(5) returns a function that adds 5. Applying that with 3 gives 5 + 3 = 8.
Q.9Hard
What is the time complexity of reducing a stream with a stateful lambda operation?
Answer: D
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.
Q.10Hard
Consider: Function<Integer, Function<Integer, Integer>> curried = a -> b -> a + b; What is this pattern called?
Answer: A
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.
Q.11Hard
Consider: Stream.of(1, 2, 3).map(x -> { System.out.println(x); return x * 2; }).collect(Collectors.toList()); What will print?
Answer: A
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).
Q.12Hard
What is the correct way to chain lambda expressions using Function interface?
Function<Integer, Integer> f1 = x -> x * 2;
Function<Integer, Integer> f2 = x -> x + 5;
How to apply f1 first, then f2?
Answer: D
Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.
Q.13Hard
Consider a scenario where you need to sort a list of strings by length. Which lambda expression is most appropriate for Comparator?
Answer: D
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.
Q.14Hard
What is the difference between a lambda expression and an anonymous inner class in Java?
Answer: D
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.
Q.15Hard
In the context of exception handling with lambda expressions, what happens if a lambda body throws a checked exception?
Answer: B
If a lambda body throws a checked exception, it must be declared in the functional interface's method signature. Otherwise, compilation will fail.
Q.16Hard
Which of the following best demonstrates the use of Optional with lambda expressions for safe null handling?
Answer: A
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.
Q.17Hard
What is the return type of a lambda expression used with IntStream.range(1, 5).map(x -> x * 2)?
Answer: C
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.