1Z0-819 Functional Interfaces 5 — Questions and Answers
Question 1: A lambda body attempts to modify a local variable declared in the enclosing method. What happens?
- The variable is silently copied into the lambda
- A compilation error occurs because local variables captured by lambdas must be effectively final (Correct answer)
- The modification is allowed only if the variable is declared volatile
- The JVM creates a new stack frame for the modification
Correct answer: A compilation error occurs because local variables captured by lambdas must be effectively final
Local variables captured by a lambda must be final or effectively final; reassigning them causes a compile-time error.
Question 2: What does the keyword `this` refer to inside a lambda expression?
- The lambda object itself
- The enclosing class instance (Correct answer)
- null
- The first parameter of the lambda
Correct answer: The enclosing class instance
Unlike anonymous classes, lambdas do not introduce a new scope; `this` refers to the enclosing class instance.
Question 3: Which statement is true about checked exceptions inside a lambda that implements `Runnable`?
- Checked exceptions can be thrown directly because Runnable's run() method allows them
- Checked exceptions must be caught or wrapped inside the lambda body (Correct answer)
- The compiler automatically converts checked exceptions to RuntimeExceptions
- Checked exceptions are ignored by the JVM inside lambdas
Correct answer: Checked exceptions must be caught or wrapped inside the lambda body
Runnable.run() does not declare any checked exceptions, so a lambda implementing it must catch any checked exceptions internally.
Question 4: Which of the following is a valid `@FunctionalInterface`?
- An interface with two abstract methods and one default method
- An interface with one abstract method that overrides `Object.toString()`
- An interface with one abstract method that does not override any Object method (Correct answer)
- An abstract class with exactly one abstract method
Correct answer: An interface with one abstract method that does not override any Object method
A functional interface must be an interface with exactly one abstract method that is not a re-declaration of an Object method.
Question 5: What is the result of compiling an interface annotated with `@FunctionalInterface` that declares two abstract methods?
- It compiles but produces a runtime exception when a lambda is assigned
- It compiles without error because @FunctionalInterface is only advisory
- A compilation error is produced (Correct answer)
- Only the first abstract method is treated as the SAM
Correct answer: A compilation error is produced
@FunctionalInterface causes the compiler to enforce the single-abstract-method constraint, producing an error if violated.
Question 6: Which scenario correctly demonstrates a functional interface used as a variable type for a lambda?
- Runnable r = (int x) -> System.out.println(x);
- Supplier<String> s = () -> "hello"; (Correct answer)
- Consumer<String> c = (x, y) -> System.out.println(x+y);
- Function<Integer> f = x -> x + 1;
Correct answer: Supplier<String> s = () -> "hello";
Supplier<String> has a single SAM getAsString() that takes no arguments and returns a String, matching () -> "hello".
Question 7: A child interface inherits one abstract method from a parent interface and declares no new abstract methods. Can it be annotated with `@FunctionalInterface`?
- No — only top-level interfaces can be functional interfaces
- Yes — it still has exactly one abstract method (inherited), satisfying the SAM requirement (Correct answer)
- No — inherited abstract methods are not counted
- Yes — but only if the parent is also annotated with @FunctionalInterface
Correct answer: Yes — it still has exactly one abstract method (inherited), satisfying the SAM requirement
An interface can be functional if it has exactly one abstract method, whether inherited or declared; the parent need not be annotated.
A lambda body attempts to modify a local variable declared in the enclosing method.
What happens?