Lambda Expressions in Java - Quiz
Total: 9 questions
1. What is lambda expression?
What is lambda expression?
Lambda expression can be called an anonymous function or a method without declaration, which can be created without belonging to any class.
2. Syntax of a lambda expression.
Syntax of a lambda expression.
(arguments) -> (body)
3. Should parameters be declared in parentheses?
Should parameters be declared in parentheses?
If there are no parameters or multiple parameters, parentheses are required.
4. Should a single parameter be declared in parenthesis?
Should a single parameter be declared in parenthesis?
It can be, but it isn't required.
5. When curly braces and 'return' keyword can be skipped for a lambda expression body?
When curly braces and 'return' keyword can be skipped for a lambda expression body?
When a body consists of a single expression.
6. Rewrite an anonymous inner class with lambda expression:
JButton button = ...
JLabel comp = ...
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comp.setText("Button was clicked.");
}
});
Rewrite an anonymous inner class with lambda expression:
JButton button = ...
JLabel comp = ...
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comp.setText("Button was clicked.");
}
});JButton button = ...
JLabel comp = ...
button.addActionListener(e -> comp.setText("Button was clicked."));
7. Rewrite code using lambda expression:
interface Function {
void call();
}
class AnonymousInnerClass {
public static void main(String []args) {
Function function = new Function() {
public void call() {
System.out.println("Hello world");
}
};
function.call();
}
}
Rewrite code using lambda expression:
interface Function {
void call();
}
class AnonymousInnerClass {
public static void main(String []args) {
Function function = new Function() {
public void call() {
System.out.println("Hello world");
}
};
function.call();
}
}interface Function {
void call();
}
class AnonymousInnerClass {
public static void main(String[] args) {
Function function = () -> System.out.println("Hello world");
function.call();
}
}
8. Can a lambda expression throw a checked exception?
Can a lambda expression throw a checked exception?
Yes, but the method in the functional interface should declare that exception.
9. Why must a local variable captured by a lambda be final or effectively final, while an instance field does not have to be?
Why must a local variable captured by a lambda be final or effectively final, while an instance field does not have to be?
A lambda captures the value of a local variable, not the variable itself. Local variables live on the method stack and disappear together with it, so the lambda gets a copy — mutating that copy would be meaningless and unsafe. Hence the rule: the variable must be final or effectively final, that is, never reassigned after initialization.
int count = 0;
cars.forEach(car -> count++); // error: local variables referenced from
// a lambda expression must be final or effectively final
Fields are reached through a reference to an object on the heap, which stays alive as long as the lambda does, so they can be modified freely. If you really need a counter, use AtomicInteger: the reference stays the same, only the contents of the object change.
AtomicInteger counter = new AtomicInteger();
cars.forEach(car -> counter.incrementAndGet());