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.422Medium
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.423Medium
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.424Medium
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.425Medium
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.
Advertisement
Q.426Medium
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.427Medium
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.428Medium
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.429Medium
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.430Medium
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.
Q.431Medium
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.432Medium
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.433Medium
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.434Medium
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.435Medium
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.
Q.436Medium
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.437Medium
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.438Medium
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.439Medium
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.440Medium
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.