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.842Hard
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.843Medium
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.844Medium
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.845Hard
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).
Advertisement
Q.846Easy
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.847Easy
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.848Medium
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.849Easy
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.850Easy
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.851Easy
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.852Medium
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.853Medium
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.
Q.854Medium
Which functional interface should be used for a lambda with no parameters and no return value?
Answer: C
Runnable is the functional interface for operations with no parameters and no return value (void).
Q.855Medium
What is the difference between Consumer and Function functional interfaces?
Answer: B
Function<T, R> takes parameter T and returns R. Consumer<T> takes parameter T and returns void.
Q.856Medium
Which of the following uses method reference correctly?
Answer: D
Both method reference (::) and lambda expression are valid ways to print list elements. Option B has incorrect syntax.
Q.857Medium
Which of the following lambda expressions will compile successfully?
Answer: D
Both A and C are valid. Option B fails because variable x is already declared as parameter. Option C uses var keyword (Java 10+).
Q.858Easy
What is the output of:
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
Answer: A
BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.
Q.859Medium
In the lambda expression (x) -> x < 10 && x > 0, what is the parameter type?
Answer: B
Parameter type must be inferred from the functional interface it's assigned to, as it's not explicitly declared.
Q.860Medium
Which of the following statements about variable scope in lambda expressions is TRUE?
Answer: B
Lambda expressions can only access local variables that are effectively final (not modified after initialization).