Java var ArrayList Reassignment Compilation in 1Z0-819

Working with Java data types Working with arrays and collections
Answer Correct answer: D — The program compiles fine; var infers ArrayList<String> for both listA and listC, so the line 13 assignment is type-safe.

Given: Which is true? - image

  1. The program fails to compile on line 10.
  2. The program fails to compile on line 13.
  3. The program fails to compile on line 11.
  4. The program compiles fine. Correct Answer

Community Votes

D
67%
B
33%

67% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The question tests var type inference with parameterized types; the common trap is misreading the lowercase l in listA as an uppercase L, 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).

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.

Community Discussion (5 comments)

bf8f3be 👍 1 Selected: B
la linea 13 la que no dice listA sino que ListA y no existe ni la variable ni la clase
ASPushkin 👍 1 Selected: D
D} even listA = listC; is good
d7bb0b2 👍 1 Selected: D
if sintax is good (listA not ListA and listB not ListB are used as name) program compile fine
d7bb0b2 👍 1
B is correct only for bad sintax. If sintax are ok (used listA and ListB). The code is valid.
Felipe47 👍 1
B is correct, not compile on line 13. because ListA, not listA. compile with error.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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

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?

Because var infers the full generic type ArrayList<String> for both listA and listC, making the assignment valid.

What if line 13 used ListA instead of listA?

Then it would be a compile error because ListA is an undeclared identifier, but that is a typo not present in the intended exam code.

More 1Z0-819 FAQ →

Related Analysis

← Back to 1Z0-819 Study Guide