1. What is a lambda expression in Java?
Answer:
Explanation:
Lambda expressions in Java are a way to create anonymous functions, which are not bound to any identifier.
2. Which interface is used with lambda expressions in Java?
Answer:
Explanation:
Lambda expressions are used with functional interfaces, which are interfaces with a single abstract method.
3. How do you identify a lambda expression in Java?
Answer:
Explanation:
Lambda expressions in Java are characterized by the arrow (->) operator.
4. Can lambda expressions access local variables of its enclosing scope?
Answer:
Explanation:
Lambda expressions can access local variables of their enclosing scope, but those variables must be final or effectively final.
5. What is the return type of a lambda expression?
Answer:
Explanation:
The return type of a lambda expression depends on the context in which it is used, particularly the functional interface it targets.
6. Can a lambda expression throw an exception?
Answer:
Explanation:
Lambda expressions can throw exceptions, but if a checked exception is thrown, it must be compatible with the exception type declared in the functional interface's abstract method.
7. What are lambda expressions primarily used for?
Answer:
Explanation:
Lambda expressions are primarily used to provide implementations for functional interfaces in a concise way.
8. How do you write a lambda expression with no parameters?
Answer:
Explanation:
A lambda expression with no parameters is written using empty parentheses followed by the arrow operator and curly braces.
9. Can lambda expressions replace anonymous inner classes?
Answer:
Explanation:
Lambda expressions can replace anonymous inner classes that implement interfaces with a single abstract method.
10. How many abstract methods can a functional interface have?
Answer:
Explanation:
A functional interface in Java can have only one abstract method.
11. Can lambda expressions be stored in variables?
Answer:
Explanation:
Lambda expressions can be assigned to variables whose type is a functional interface.
12. What is a method reference in Java?
Answer:
Explanation:
Method references are an alternative, shortened syntax to lambda expressions used to refer directly to methods.
13. Which of these is a valid lambda expression in Java?
Answer:
Explanation:
The valid syntax for a lambda expression with two parameters is enclosing them in parentheses followed by the arrow operator and the expression.
14. How are lambda expressions compiled in Java?
Answer:
Explanation:
Internally, lambda expressions are compiled into anonymous inner classes by the Java compiler.
15. Can lambda expressions modify local variables from their enclosing scope?
Answer:
Explanation:
Lambda expressions cannot modify local variables from their enclosing scope; they can only read final or effectively final variables.
16. What is the purpose of the @FunctionalInterface annotation?
Answer:
Explanation:
The @FunctionalInterface annotation is used to indicate that an interface is intended to be a functional interface, i.e., it should have exactly one abstract method.
17. What happens if the body of a lambda expression has multiple statements?
Answer:
Explanation:
If a lambda expression contains more than one statement, its body must be enclosed in curly braces.
18. In lambda expressions, what does the double colon (::) operator represent?
Answer:
Explanation:
The double colon (::) operator in lambda expressions is used for method references, providing a shorthand notation for a lambda expression calling a method.
19. How can lambda expressions access external non-final variables?
Answer:
Explanation:
Lambda expressions can access external non-final variables by passing them as parameters. Otherwise, they can only access final or effectively final variables from their enclosing scope.
20. What type of parameters can a lambda expression have?
Answer:
Explanation:
Lambda expressions can have typed parameters, untyped parameters (inferred from context), or no parameters at all.
21. What is the primary advantage of lambda expressions?
Answer:
Explanation:
Lambda expressions allow for more concise and readable code, especially when implementing single-method interfaces.
22. Which of the following is a built-in functional interface in Java?
Answer:
Explanation:
Runnable is a built-in functional interface in Java that is often used with lambda expressions for creating threads.
23. Can lambda expressions be passed as arguments to a method?
Answer:
Explanation:
Lambda expressions can be passed as arguments to methods, particularly those that expect a functional interface as a parameter.
24. How do lambda expressions facilitate functional programming in Java?
Answer:
Explanation:
Lambda expressions facilitate functional programming by allowing methods to be treated as lambda expressions, or first-class citizens, which can be passed around as arguments or returned from methods.
25. Can lambda expressions be used to create instances of an interface?
Answer:
Explanation:
Lambda expressions can be used to provide an implementation of a functional interface, effectively creating an instance of that interface.