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.22Medium
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.23Medium
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.
Q.24Medium
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.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.29Medium
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.30Medium
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.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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.36Medium
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).
Q.37Medium
Consider: Comparator<String> comp = (s1, s2) -> s2.compareTo(s1);
What does this lambda expression do?
Answer: B
By reversing the compareTo order (s2 compared to s1), it sorts in descending order.
Q.38Medium
Which lambda expression is equivalent to the method reference Integer::parseInt?
Answer: A
Method reference Integer::parseInt is equivalent to lambda (s) -> Integer.parseInt(s).
Q.39Medium
Analyze: What is the output?
BiConsumer<String, String> printer = (a, b) -> System.out.println(a + b);
printer.accept("Java", "8");
Answer: A
BiConsumer accepts two parameters and performs the action. String concatenation of "Java" + "8" produces "Java8".
Q.40Medium
What is the return type of a lambda expression (x, y) -> x + y where x and y are integers?
Answer: C
The return type of a lambda expression is inferred from the functional interface it's assigned to. The same lambda can return Integer or int depending on the context.