Effectively Final Variable Usage in Java Lambda Expressions
Given: What is the result? - 
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
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 asfinal, 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 ofs 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?
Final requires explicit declaration; effectively final means the compiler infers it because the value is never reassigned.
Can I modify the array object referenced by an effectively final variable?
Yes. Effectively final restricts reassignment of the reference, not modification of the object's internal state.