Learn Java 8 Lambda expressions with examples at Lambda Expressions guide.
1. What is a Lambda Expression?
The main use of Lambda expression is to provide an implementation for functional interfaces.
Syntax of Lambda Expression
(parameters) -> expression
(parameters) -> { statements; }
Example
@FunctionalInterface
interface MyFunction {
int apply(int x, int y);
}
MyFunction add = (x, y) -> x + y;
int result = add.apply(5, 3); // result is 8
2. Why use Lambda Expression?
3. Explain Lambda Expression Syntax
(parameters) -> expression
(parameters) -> { statements; }
interface Addable{ int add(int a,int b); }
Addable withLambdaD = (int a,int b) -> (a+b); System.out.println(withLambdaD.add(100,200));
4. Which of the following are valid lambda expressions?
A.
String a, String b -> System.out.print(a+ b);
B.
() -> return;
C.
(int i) -> i;
D.
(int i) -> i++; return i;
Answer
The correct answer is C.
Explanation
Option C is valid. The body doesn't need to use the return keyword if it only has one statement.
5. Write a Java Lambda Expression to Create a Thread
Runnable task = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Running inside the thread: " + i);
}
};
Thread myThread = new Thread(task);
myThread.start();
Thread myThread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("Running inside the thread: " + i);
}
});
myThread.start();
6. How are Lambda Expressions and Functional Interfaces related?
The main use of Lambda expression is to provide an implementation for functional interfaces.A lambda expressions provide a compact and expressive way to create instances of functional interfaces, implementing the interface's single abstract method with the body of the lambda. This relationship promotes a more functional programming style in Java, allowing for more concise and readable code.
For example, Lambda expression provides an implementation for a Printable functional interface:
interface Printable {
void print(String msg);
}
public class JLEExampleSingleParameter {
public static void main(String[] args) {
// without lambda expression
Printable printable = new Printable() {
@Override
public void print(String msg) {
System.out.println(msg);
}
};
printable.print(" Print message to console....");
// with lambda expression
Printable withLambda = (msg) -> System.out.println(msg);
withLambda.print(" Print message to console....");
}
}
Output : Print message to console....
Print message to console....
interface Printable {
void print(String msg);
}
public class JLEExampleSingleParameter {
public static void main(String[] args) {
// without lambda expression
Printable printable = new Printable() {
@Override
public void print(String msg) {
System.out.println(msg);
}
};
printable.print(" Print message to console....");
// with lambda expression
Printable withLambda = (msg) -> System.out.println(msg);
withLambda.print(" Print message to console....");
}
}
Print message to console....
Print message to console....
7. Explain the Various Forms of Writing Lambda Expressions.
No Parameters, No Return Value:
() -> System.out.println("Hello, World!");
Single Parameter, No Type Declaration:
s -> System.out.println(s);
Single Parameter, With Type Declaration:
(String s) -> System.out.println(s);
Multiple Parameters:
(a, b) -> a + b;
(int a, int b) -> a + b;
Return Statement:
(int a, int b) -> a + b;
(int a, int b) -> {
int sum = a + b;
return sum;
};
No Parameters, Return Value:
() -> 42;
Use Local Variables:
int factor = 2;
(int a) -> a * factor;
8. How is Lambda Expression Different from Anonymous Class?
Lambda expressions provide a more concise way to implement functional interfaces, whereas anonymous classes are more verbose. Lambdas don’t create a separate class file or object.
Here’s a clear comparison:

9. Can Lambdas Access Local Variables?
Yes, lambda expressions can access final or effectively final local variables from the enclosing scope. This means the variable should not be modified after it’s used inside the lambda.
Example:
String prefix = "Hello ";
Consumer<String> c = name -> System.out.println(prefix + name);
c.accept("Ravi");
If you try to change prefix
later, the compiler will throw an error.
10. What is the Difference Between Lambda and Method Reference?
Both lambda expressions and method references are used to provide implementations for functional interfaces. A method reference is a shorter way to write certain lambdas that call existing methods.
Here’s a detailed comparison:

Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in an Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Comments
Post a Comment
Leave Comment