In Java streams, what does the filter() method use?
Answer: A
filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.
Q.402Medium
What will be the result of executing: List<Integer> nums = Arrays.asList(1, 2, 3, 4); nums.stream().map(x -> x * 2).collect(Collectors.toList());
Answer: A
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
Q.403Medium
Consider: Function<Integer, Integer> f = x -> x * x; What will f.apply(5) return?
Answer: A
Function<Integer, Integer> takes an Integer and returns an Integer. f.apply(5) computes 5 * 5 = 25.
Q.404Medium
What is the purpose of the Optional class when used with lambda expressions in Stream operations?
Answer: A
Optional is used to handle absence of values gracefully. Methods like ifPresent(lambda) and orElse() help prevent null pointer exceptions.
Q.405Medium
Which of the following lambda expressions is INCORRECT?
Answer: C
In option C, you cannot specify types in lambda parameters when using implicit typing. It should be either (x, y) or explicitly define all parameters.
Advertisement
Q.406Medium
Consider: Stream.of(1, 2, 3, 4, 5).filter(x -> x > 2).map(x -> x * x). What will the terminal operation need to be?
Answer: C
Both collect() and forEach() are valid terminal operations. collect() returns a list, forEach() performs an action. reduce() is also valid but would sum the squares.
Q.407Medium
What is method reference (::) in Java and how does it relate to lambda expressions?
Answer: A
Method reference (::) is syntactic sugar for lambda expressions. System.out::println is equivalent to x -> System.out.println(x).
Q.408Medium
What is the difference between peek() and forEach() when used with lambda expressions in Streams?
Answer: A
peek() is an intermediate operation used for debugging; it doesn't end the stream. forEach() is a terminal operation that consumes the stream.
Q.409Medium
In the lambda expression (a, b) -> a.compareTo(b), what is the implicit functional interface?
Answer: A
The Comparator<T> functional interface has the method compare(T a, T b) which returns an int, matching this lambda's behavior.
Q.410Medium
What will be the output of: IntStream.range(1, 4).map(x -> x * 2).sum();
Answer: A
range(1, 4) generates [1, 2, 3]. After map(x -> x * 2): [2, 4, 6]. Sum = 2 + 4 + 6 = 12.
Q.411Medium
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.412Medium
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.413Medium
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.414Medium
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.415Medium
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.416Medium
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.417Medium
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.418Medium
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.419Medium
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.420Medium
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.