Effectively Final Variable Usage in Java Lambda Expressions
Short answer
C The program prints "Java" because the local variable is effectively final.
Question

Key Takeaways
The exam tests the 'effectively final' constraint for lambda captures; the trap is assuming any reference variable assignment causes a compilation error, ignoring that immutability allows access.
This question tests the rule that local variables accessed by lambda expressions must be effectively final. The code compiles and runs correctly because the variable is not modified after initialization.
Common mistake: Candidates often choose Compilation Error (D) because they confuse the requirement for explicit `final` with the broader concept of 'effectively final', failing to notice the variable isn't reassigned.
Expert Analysis
Why the Answer Is Correct
The correct answer is C (Java). In Java, a local variable used inside a lambda expression does not need to be explicitly declared as final, provided it is 'effectively final'. A variable is effectively final if its value is initialized once and never changed thereafter. Since the string s is assigned "Oracle" and never reassigned, the compiler accepts the lambda -> System.out.println(s).
Why the Other Options Are Wrong
Option A (Oracle) is incorrect because the lambda prints the value of s at execution time, which is "Java", not "Oracle". Option B is incorrect because no runtime exception occurs; the code is valid. Option D (Compilation Error) is the most common distractor; it is wrong because the restriction only applies to variables whose values change, or to parameters that are reassigned within the lambda body.
Community Comment Notes
Community consensus strongly supports the compilation success. One user noted that "Local variables... can be accessed in a lambda expression provided and they are effectively final," confirming the logic. Another commenter emphasized that "If s is modified then code not compile," highlighting the conditional nature of the rule.
Exam Strategy
Always check if a captured variable is reassigned anywhere in its scope. If it is not, it is effectively final and legal in lambdas. Do not assume all local variables require an explicit final keyword.
Frequently Asked Questions
What is the difference between final and effectively final?
Can I modify the array object referenced by an effectively final variable?
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 3 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 3
-
ASPushkin 👍 2right answer is D Local variables decalred in the enclosing method, including its formal parameters, can be accessed in a lambda expression provided and they are effectively final This one : String s = "Oracle"; Runnable r = () -> { System.out.println(s); }; //s = "Java"; Thread t = new Thread(r); t.start() will be ok
-
d7bb0b2 👍 1 Answered Dvariables in lambdas must be a final or effectively final. If s is modified then code not compile
-
Omnisumem 👍 2 Answered Dtested
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.