Lambda Expressions - Quiz

Total: 8 questions

1. 

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.

(arguments) -> (body)
3. 

Should parameters be declared in parentheses?

If the are no parameters or multiple parameters, parentheses are required.

4. 

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 a body consists of a single expression or a statement block.

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.");
    }
});
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();
    }
}
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?

Yes, but the method in the functional interface should declare that exception.

Page 1 of 1