This post contains a few useful Java lambda expressions and multiple-choice questions to self-test your knowledge of Java 8 lambda expressions. The answer and explanation have been given for each MCQ.
1. Which interface type can lambda expressions be used for in Java?
Answer:
Explanation:
Lambda expressions are primarily designed to represent instances of functional interfaces. A functional interface has just one abstract method and may contain one or several default or static methods.
2. Which symbol denotes a lambda expression in Java?
Answer:
Explanation:
In Java, lambda expressions are denoted by the arrow token "->".
3. What will the following lambda expression return?
(x, y) -> x + y
Answer:
Explanation:
The lambda expression defines a function that takes two parameters and returns their sum.
4. Lambda expressions can be used to replace ...
Answer:
Explanation:
Lambda expressions can be used to replace instances of anonymous classes that implement a functional interface.
5. Which of the following is a valid lambda expression?
Answer:
Explanation:
All the given options are valid lambda expressions in Java.
6. Lambda expressions let you ...
Answer:
Explanation:
Lambda expressions allow you to express instances of single-method interfaces (functional interfaces) in a concise manner.
7. Lambda expressions can be used with the Java Collections API primarily in ...
Answer:
Explanation:
Lambda expressions, combined with streams, can be used in various operations like sorting, iterating, and filtering in the Java Collections API.
8. The scope of variables in a lambda expression is ...
Answer:
Explanation:
Variables within lambda expressions have the same scope as the enclosing method or block. Also, lambdas can access the final or effectively final variables from the enclosing scope.
9. Which among the following is not a functional interface in Java?
Answer:
Explanation:
List is not a functional interface. It's an interface representing a collection of elements.
10. You cannot use ... inside lambda expressions.
Answer:
Explanation:
Lambda expressions don't support the use of break and continue statements if they're not part of a loop within the lambda.
11. Which Java feature works effectively with lambda expressions?
Answer:
Explanation:
Java's Stream API works effectively with lambda expressions, simplifying operations on data sets.
12. Lambda expressions can throw exceptions.
Answer:
Explanation:
Lambda expressions can throw exceptions, but if an exception is checked, it must be compatible with the exceptions listed in the throws clause of the functional interface method.
13. What does the following lambda expression represent?
() -> {}
Answer:
Explanation:
The lambda expression () -> {} represents a no-op (does nothing) lambda with no parameters and returns void.
14. Which of the following statements are true?
Answer:
Explanation:
A return keyword is not always required (or optional) in a lambda expression. It depends on the signature of the functional interface method. Curly brackets are required whenever the return keyword is used in a lambda expression. Both can be omitted if the lambda expression's body is just one statement.
15. How is this keyword handled inside a lambda expression?
Answer:
Explanation:
For a lambda expression, this resolves to the enclosing class where the lambda is written.