Java var ArrayList Reassignment Compilation in 1Z0-819
Short answer
D The program compiles fine; var infers ArrayList<String> for both listA and listC, so the line 13 assignment is type-safe.
Question

Key Takeaways
The question tests
vartype inference with parameterized types; the common trap is misreading the lowercaselinlistAas an uppercaseL, or wrongly assuming generic invariance blocks the assignment.
This 1Z0-819 question tests local variable type inference with generic collections: var listA = new ArrayList<String>() and var listC = new ArrayList<String>() compile fine, and the reassignment listA = listC on line 13 is valid. The program compiles without errors (D).
Common mistake: The most common wrong answer is B (fails on line 13), chosen because learners misread `listA` as `ListA` or mistakenly believe that two `ArrayList<String>` variables cannot be assigned to each other.
Expert Analysis
Why the Answer Is Correct
The code uses var to declare listA, listB, and listC as ArrayList<String> (or compatible types). Line 10 declares listA with no error, line 11 declares listB with no error, and line 13's assignment listA = listC; is valid because both variables have the same inferred parameterized type. var in Java 10+ infers the exact static type from the initializer, including generics, so listA and listC are both ArrayList<String>. The program therefore compiles fine, making D the correct answer.
Why the Other Options Are Wrong
Option A (fails on line 10) is wrong because var listA = new ArrayList<String>; is a perfectly legal declaration; var can be used with parameterized constructors. Option C (fails on line 11) is wrong for the same reason; var listB = new ArrayList<Integer>; also compiles. Option B (fails on line 13) would only be correct if line 13 used an undeclared identifier like ListA (capital L) or if listA and listC had incompatible generic types, but neither is the case in the intended code. The assignment of a List<String> to an ArrayList<String> would fail, but here both are ArrayList<String>.
Community Comment Notes
Many learners, such as d7bb0b2, correctly noted that "if syntax is good (listA not ListA...) program compile fine." ASPushkin similarly commented that "even listA = listC; is good." Some learners like bf8f3be read line 13 as "ListA" and voted B, but this appears to be a misreading of the image's font; the intended code uses lowercase listA. The consensus among those who read the variable name correctly is that the program compiles without error.
Official Reference
- https://docs.oracle.com/en/java/javase/11/language/local-variable-type-inference.html
- https://docs.oracle.com/javase/specs/jls/se11/html/jls-14.html#jls-14.4
These links open the official vendor documentation in a new tab.
Exam Strategy
When a question shows var with generic collections, verify the inferred type of each variable from its initializer. Don't let a blurry image or a case-sensitivity typo distract you from the actual type compatibility rules.
Frequently Asked Questions
Why does line 13 compile when using var with ArrayList?
What if line 13 used ListA instead of listA?
Community Votes
67% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 5 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 5
-
d7bb0b2 👍 1 Answered Dif sintax is good (listA not ListA and listB not ListB are used as name) program compile fine
-
d7bb0b2 👍 1B is correct only for bad sintax. If sintax are ok (used listA and ListB). The code is valid.
-
bf8f3be 👍 1 Answered Bla linea 13 la que no dice listA sino que ListA y no existe ni la variable ni la clase
-
Felipe47 👍 1B is correct, not compile on line 13. because ListA, not listA. compile with error.
-
ASPushkin 👍 1 Answered DD} even listA = listC; is good
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.