Consider a lambda expression: (String s) -> s.length(). What is the correct functional interface for this?
Answer: A
Function<String, Integer> takes a String input and returns an Integer (the length). This matches the lambda that takes a String parameter and returns s.length().
Q.42Medium
What will happen if you try to access a local variable from an enclosing scope that is not final or effectively final in a lambda expression?
Answer: B
Lambda expressions can only access local variables that are final or effectively final. This is because lambda expressions are translated to methods that need access to stable variable values.
Q.43Medium
Consider the code: List<String> names = Arrays.asList("Alice", "Bob"); names.forEach(name -> System.out.println(name)); What type of functional interface is used in forEach?
Answer: B
forEach accepts a Consumer functional interface. Consumer<T> takes an input and performs an action without returning anything, which matches the System.out.println action.
Q.44Medium
What is the output of: List<Integer> nums = Arrays.asList(1,2,3); nums.stream().filter(x -> x > 1).forEach(System.out::println);
Answer: B
filter(x -> x > 1) keeps only elements greater than 1, which are 2 and 3. These are then printed on separate lines using forEach.
Q.45Medium
Which lambda expression correctly implements a custom functional interface: public interface Math { int calculate(int a, int b); }
Answer: D
Both expressions are valid. Option A uses type inference while Option C explicitly specifies types. Both can implement the calculate method.
Advertisement
Q.46Medium
Consider: Comparator<Integer> comp = (a, b) -> b - a; List<Integer> list = Arrays.asList(3,1,2); Collections.sort(list, comp); What is the result?
Answer: B
The comparator (a, b) -> b - a returns negative values when b < a, causing elements to be sorted in descending order. Result is [3, 2, 1].
Q.47Medium
Which of the following correctly uses a method reference as an alternative to a lambda expression?
Answer: D
All three are valid method references that can replace their equivalent lambda expressions. Method references are a shorthand notation introduced in Java 8.
Q.48Medium
Which stream operation uses a lambda expression to transform elements from one type to another?
Answer: B
map() is used to transform elements. It takes a Function lambda that converts each element from one type/value to another.
Q.49Medium
Which of the following lambda expressions is INVALID in Java?
Answer: D
In a lambda expression, if the body uses curly braces, 'return' keyword is required. But without curly braces, 'return' keyword cannot be used. Option D is invalid because it has 'return' without curly braces.
Q.50Medium
A stream processes a list of strings and applies a lambda to convert them to uppercase. Which intermediate operation should be used?
Answer: B
The map() intermediate operation transforms each element using the provided lambda function. filter() returns boolean, peek() doesn't transform, and collect() is a terminal operation. map() is the correct choice for transformation.
Q.51Medium
Consider the following code:
List<String> fruits = Arrays.asList("apple", "banana", "cherry");
fruits.removeIf(s -> s.length() > 5);
What will be the contents of 'fruits' list after execution?
Answer: B
removeIf() removes all elements that satisfy the lambda predicate. Strings with length > 5 are "banana" (6 chars) and "cherry" (6 chars). Only "apple" (5 chars) remains as its length is not greater than 5.