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.62Medium
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.63Medium
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.64Medium
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.65Easy
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.
Advertisement
Q.66Medium
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.67Medium
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.68Easy
What is the result of this code?
UnaryOperator<Integer> square = x -> x * x;
System.out.println(square.apply(5));
Answer: A
UnaryOperator applies the operation x * x where x = 5, resulting in 25.
Q.69Easy
Which of the following best describes a functional interface?
Answer: B
A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.
Q.70Easy
What will be the output of:
Supplier<String> greeting = () -> "Hello";
System.out.println(greeting.get());
Answer: A
Supplier.get() returns the value produced by the lambda expression, which is "Hello".
Q.71Medium
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.72Medium
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.73Hard
What is the correct way to chain lambda expressions using Function interface?
Function<Integer, Integer> f1 = x -> x * 2;
Function<Integer, Integer> f2 = x -> x + 5;
How to apply f1 first, then f2?
Answer: D
Both andThen and explicit composition order result in applying f1 first (3*2=6, then 6+5=11), equivalent to option C.
Q.74Medium
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.75Easy
Which of the following is a valid functional interface that can be used with lambda expressions?
Answer: A
A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.
Q.76Medium
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.77Easy
Which annotation is used to mark an interface as a functional interface in Java?
Answer: B
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
Q.78Medium
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.79Medium
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.80Easy
Which of the following lambda expressions is syntactically incorrect?
Answer: C
The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.