Lambda Expressions — Concept Check
Questions on Lambda Expressions. Write concise, inline function objects with lambda syntax — the foundation of functional-style Java introduced in Java 8.
Questions in this quiz
8 questions on Lambda Expressions — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In Java, which of the following lambda expressions correctly captures a local variable?
- A(x) -> x * 2, with no local variable capture
- B(x) -> x + localVar, where localVar is modified after capture
- C(x) -> x - staticField, where staticField is a static variable
- D(x) -> x + instanceField, where instanceField is an instance variable
2.A developer writes a lambda expression in Java: `(a, b) -> a + b`. What is required for this lambda to compile?
- AThe lambda must be assigned to an interface with multiple methods.
- BThe lambda must be assigned to a class with a method that takes two parameters.
- CThe lambda must be assigned to a functional interface with a method that takes two parameters.
- DThe lambda must explicitly declare parameter types.
3.Given the lambda expression `s -> System.out.println(s)`, which of the following is true?
- AThe lambda must be enclosed in parentheses: `(s) -> System.out.println(s)`.
- BThe lambda cannot be used with any functional interface.
- CThe lambda can only be used with a functional interface that takes a String parameter.
- DThe lambda must include a return statement.
4.A developer uses a lambda expression in a multithreaded Java application. What must they ensure about captured local variables?
- AThey must be synchronized to prevent race conditions.
- BThey must be volatile to ensure visibility across threads.
- CThey must be final or effectively final.
- DThey must be static to avoid capture issues.
5.In a Java program, a lambda expression is used as an argument to a method. What is a potential benefit of this approach?
- AIt reduces the number of .class files generated.
- BIt allows the lambda to implement multiple interfaces.
- CIt allows the method to modify the lambda's captured variables.
- DIt provides access to the lambda's 'this' as its own instance.
6.What happens if you try to modify a local variable captured by a lambda in Java?
- AThe code compiles but throws an exception at runtime.
- BThe code compiles and runs without issues.
- CThe code fails to compile due to variable capture rules.
- DThe code requires the variable to be declared as volatile.
7.A developer mistakenly believes a lambda expression has its own 'this'. What is the actual behavior?
- A'this' in a lambda refers to the first parameter.
- B'this' in a lambda is undefined.
- C'this' in a lambda refers to the enclosing class instance.
- D'this' in a lambda refers to the lambda's own instance.
8.Which of the following is true about lambda expressions and anonymous classes in Java?
- ALambdas can only implement functional interfaces.
- BAnonymous classes cannot use 'this' to refer to their own instance.
- CLambdas are always more efficient than anonymous classes.
- DLambdas can implement any interface or extend a class.