Which Java feature was introduced specifically to support generics compilation?
Answer: A
Bridge methods are synthetic methods generated during compilation to maintain polymorphism with generic types and type erasure.
Q.182Hard
A method processes collections where items need to be added back to the same collection type. Which wildcard approach ensures type safety for write operations?
Answer: A
'? super T' allows writing T instances (contravariant), while '? extends T' allows safe reading (covariant). This combination enables flexible yet type-safe collection manipulation.
Q.183Hard
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.184Hard
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.185Hard
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 -> ...).
Advertisement
Q.186Hard
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.187Hard
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.
Q.188Hard
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.189Hard
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.190Hard
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.191Hard
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.192Hard
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.193Hard
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.194Hard
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.195Hard
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.196Hard
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.197Hard
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.198Hard
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.199Hard
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.
Q.200Hard
An enterprise application needs different bean implementations based on environment (dev/prod). Which approach is most suitable?
Answer: C
@Profile allows defining beans for specific environments (dev, prod, test). Alternatively, @Conditional provides more granular control with custom conditions.