1Z0-811 · Question #53
Given: public class TestFinal { final int 1 = 5; static void modify (TestFinal test) { test.1 = 99; } public static void main (String [] args) { final TestFinal tf = new TestFinal (); modify (tf)…
The correct answer is D. A compilation error occurs in the main method. Note: There appears to be a formatting issue in this question - the variable named 1 (digit) is almost certainly i (letter), a common OCR/font rendering error in exam transcriptions. The analysis below uses i. --- Why the answer is questionable: Based on the code as shown, A is…
Question
Options
- AA compilation error occurs in the modify method.
- B99
- C5
- DA compilation error occurs in the main method.
How the community answered
(31 responses)- B3% (1)
- C3% (1)
- D94% (29)
Explanation
Note: There appears to be a formatting issue in this question - the variable named 1 (digit) is almost certainly i (letter), a common OCR/font rendering error in exam transcriptions. The analysis below uses i.
Why the answer is questionable: Based on the code as shown, A is actually the more defensible answer. The line test.i = 99 inside modify attempts to reassign a final instance field, which the Java compiler rejects at that exact line - inside modify, not main.
For D to be correct, main would need to try to reassign the final reference itself, e.g., tf = new TestFinal(). The final keyword on a reference variable (final TestFinal tf) only prevents the reference from pointing to a new object - it does not prevent the object's own fields from being changed. So calling modify(tf) is syntactically legal from main's perspective; the error is inside modify.
Why the distractors are wrong:
- B (99) and C (5) are impossible - the code doesn't compile, so no output is produced.
- A is where the logical compile error lives (
test.i = 99modifies afinalfield), but the provided answer key marks it wrong - likely because the original question included a reassignment oftfinmainthat didn't survive transcription.
Memory tip: Think of final on a reference as a locked steering wheel (you can't turn toward a new object), but the engine inside the object can still run. final on a field locks the value itself. When you see final on a local variable in main, ask: is the reference being reassigned? That's what triggers a compile error there.
Topics
Community Discussion
No community discussion yet for this question.