We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
LAMBDA EXPRESSIONS
Q1. What are lambda expressions?
Answer: A lambda expression in Java is a feature that enables you to create an implementation of a functional interface (an interface with a single method) in a more simple way. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method. It consists of parameters, an arrow (->), and a body. Note: When you answer this question make sure to use the term "functional interface" this will give a positive impression to the interviewer.
Q2. Syntax and components of lambda expressions.
Answer: The syntax of a lambda expression consists of three main components: First: Parameters: It may or may not have parameters depending on our code. Second: Arrow Operator (->): This symbol shows where the input parameters end and the body of the lambda expression starts. Third: Body: The code that defines what the lambda does. It can be a single expression or a block of statements enclosed in curly braces. Note: We request you to search in youtube about realistic coding examples for different parameters (zero, single, multiple), that will be even more helpful in terms of interview.
Q3. Difference between anonymous classes and lambda expressions?
Answer: Anonymous classes allow us to create a new class with more complex behavior, including multiple methods and state, which can be useful in many situations. However, they tend to be more verbose and require more boilerplate code. On the other hand, lambda expressions are a more concise way to implement single-method interfaces. They make our code cleaner and easier to read since we can write them in just a few lines. However, they can only be used for interfaces with a single method only. Note: While anonymous classes have broader usability, we encourage the use of lambda expressions wherever possible. This not only simplifies our code but also maintains consistency, especially when working with the Stream API.
Q4. When to use lambda expressions in java?
Answer: We should use lambda expressions in Java when: 1. Functional Interfaces: When we need to implement a single-method interface, like `Runnable`, `Callable`, or any custom functional interfaces. 2. Concise Code: When we want to make our code cleaner and more readable by reducing boilerplate. 3. Collections and Streams: When working with Java Collections or Stream APIs, lambda expressions allow us to write more expressive code for filtering, mapping, or reducing data. 4. Event Handling: When defining event listeners in GUI applications. 5. Callbacks: When passing behavior as parameters to methods, such as in asynchronous programming or when using frameworks that require callback functions. Note: We can use lambda expressions to improve code readability and reduce the amount of boilerplate when dealing with single-method interfaces. Q5. What is a functional interface? Answer. A functional interface is an interface which contains exactly one abstract method. This concept is important for enabling the use of lambda expressions. Functional interfaces can have multiple default or static methods, but only one abstract method is required. Examples of functional interfaces are:
● Runnable (with the method run())
● Callable (with the method call()) ● Predicate (with the method test(T t)) ● Function (with the method apply(T t)) ● Consumer (with the method accept(T t))
Note: When you are asked “What is something?” don't just stop after defining it, explain its use cases, annotation and examples as well.
Q6. Explain @FunctionalInterface annotations.
Answer: The @FunctionalInterface annotation is a marker annotation that indicates an interface is supposed to be a functional interface. While it's not strictly required to declare functional interface, using this annotation provides several benefits like compile_time_checking and documentation. @FunctionalInterface public interface MyFunction { void apply(String input); } Note: Practice with Collection.sort(), forEach(), map(), filter() for better understanding.
Q7. How are lambda expressions used in the Stream API?
Answer: Lambda expressions are integral to the Stream API. Lambda expressions relate to the Stream API because it allows us to define the operations performed on data within streams. They enable us to specify how each element in a stream should be processed, such as filtering or transforming data, directly as part of the method calls. This makes it easier to write clear, concise, and expressive code when working with collections. It can be used for Functional Operations, Enhanced Readability, Chaining Operations. Note: We encourage you to learn the Stream API. Practice common operations like filter(), map(), reduce(), collect(), etc.
Q8. How do method references differ from lambda expressions?
Answer: Method references are a concise way to refer to existing methods, whereas lambda expressions define inline logic. Both achieve the same result, but method references are used when the lambda's body would just call an existing method, making the code more readable.For example: // Lambda expression list.forEach(s -> System.out.println(s)); // Method reference list.forEach(System.out::println); Note: Learn about method reference and construction reference. Q9. What variables can be accessed in lambda expressions? Answer: Lambda expressions can access instance variables, static variables, and local variables. However, local variables must be effectively final, meaning their value can't change after being assigned. Note: final int num = 10; // this is final because it uses ‘final’ keyword int num = 10; // this is effectively final, because it does not use the ‘final’ keyword but has been hardcoded. Learn about the scope of lambda. Interview Questions Q1. What is the lambda expression? Q2. Syntax of lambda expression. Q3. Explain Various Forms of Writing Lambda Expression? {no, single, multiple parameter} Q4. Why use lambda expressions? Q5. Write a Java Lambda Expression to Create a Thread Q6. What is a functional interface? Q7. How Lambda Expression and Functional Interfaces are Related? Q8. What is method reference? Q9. When can you replace lambda expression with method reference? Q10. Can you use local variables inside lambda expressions? Q11. Difference between final and effectively final Q12. Can you name some predefined common functional interfaces of JDK 8, which is marked as functional interface? Q13. What is type inference in lambda expression? Q14. What is difference between map and flatmap, also write example using lambda expression Q15. What is the difference between lambda expression and anonymous class? Q16. What are the merits and demerits of lambda expression?