Which functional interface is used for operations that take two arguments and return a single value?
Answer: B
BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.
Q.42Easy
What is a method reference (::) in Java?
Answer: A
Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.
Q.43Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * 2).forEach(System.out::println); What will be the output?
Answer: A
filter(x -> x > 2) keeps 3, 4, 5. map(x -> x * 2) transforms them to 6, 8, 10. forEach prints each value.
Q.44Medium
What is the difference between peek() and forEach() in streams?
Answer: B
peek() is an intermediate operation that returns a stream for chaining, useful for debugging. forEach() is terminal and returns void, ending the stream chain.
Q.45Medium
What issue can arise when using lambda expressions with checked exceptions?
Answer: B
Since standard functional interfaces don't declare checked exceptions, you must either wrap in try-catch or create a custom functional interface that throws the checked exception.
Advertisement
Q.46Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * x).forEach(System.out::print);
Answer: A
range(1, 4) generates 1, 2, 3. map(x -> x * x) produces 1, 4, 9. Printed without spaces gives '149'.
Q.47Medium
In the lambda expression (a, b) -> a.compareTo(b), what can we infer about the parameters?
Answer: D
Without explicit type declarations or context, the compiler must infer types from usage. We can only determine they have a compareTo() method, likely Comparable types.
Q.48Hard
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.49Hard
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.50Medium
Which of the following correctly uses method reference with constructor?
Answer: A
Constructor method references use ClassName::new syntax. String::new refers to the String constructor and can be used wherever a Supplier<String> is expected.
Q.51Medium
What happens when a lambda expression references a local variable from its enclosing scope?
Answer: A
Lambda expressions can only access local variables that are final or effectively final (not reassigned). This ensures thread-safety and consistency.
Q.52Hard
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.53Easy
What is the purpose of the @FunctionalInterface annotation in Java?
Answer: B
@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.
Q.54Easy
Which stream operation would you use to transform elements from one type to another?
Answer: B
map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.
Q.55Medium
What will be the output of: List<Integer> list = Arrays.asList(1, 2, 3); list.replaceAll(x -> x * 2); System.out.println(list);
Answer: B
replaceAll() with a lambda expression modifies the list in-place, multiplying each element by 2. Result is [2, 4, 6].
Q.56Easy
Which of the following is a valid lambda expression in Java?
Answer: D
All three are valid lambda expression syntaxes with different parameter types and return statements.
Q.57Easy
A lambda expression can only be used with which type of interface?
Answer: B
Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.
Q.58Easy
What is the return type of the lambda expression: (a, b) -> a > b?
Answer: B
The expression uses comparison operator (>), which returns a boolean value.
Q.59Medium
Which of the following lambda expressions has incorrect syntax?
Answer: D
Ternary operator inside lambda requires braces and explicit return statement for type inference.
Q.60Medium
What will be the type of the lambda expression (x) -> x.toString()?
Answer: C
Without context, the parameter type cannot be inferred. The functional interface type determines the parameter type.