1Z0-819 · Question #163
public class Test { private String strings; } Which two construct ors will compile and set the class field strings? (Choose two.)
The correct answer is C. public Test(String... strings) { this.strings = strings; } E. public Test(String[] strings) { this.strings = strings; }. Options C and E are correct because the field strings is of type String[] (the brackets appear lost in formatting), and both constructors assign a compatible String[] value to it. In option C, the varargs parameter String... strings is syntactic sugar for String[] - the…
Question
Options
- Apublic Test(List<String> strings) { this.strings = strings; }
- Bpublic Test(String... strings) { this.strings = strings; }
- Cpublic Test(String... strings) { this.strings = strings; }
- Dpublic Test(String strings) { this.strings = strings; }
- Epublic Test(String[] strings) { this.strings = strings; }
How the community answered
(47 responses)- A4% (2)
- B6% (3)
- C79% (37)
- D11% (5)
Explanation
Options C and E are correct because the field strings is of type String[] (the brackets appear lost in formatting), and both constructors assign a compatible String[] value to it. In option C, the varargs parameter String... strings is syntactic sugar for String[] - the compiler treats them identically at the bytecode level, so the assignment this.strings = strings is valid. In option E, String[] strings explicitly declares an array parameter, which directly matches the field type.
Why the others are wrong:
- A passes a
List<String>, which is not assignment-compatible withString[]- you'd needtoArray()to convert it. - B appears identical to C as printed, suggesting a typo in the question; treat it as a distractor (perhaps the original had a different body or missing assignment).
- D passes a single
Stringscalar, which cannot be assigned to aString[]field without wrapping (e.g.,new String[]{strings}).
Memory tip: Varargs (T...) and array (T[]) are interchangeable as parameter types for assignment purposes - both are T[] under the hood. When you see a String[] field, any constructor that supplies a String[] (explicitly or via varargs) will compile cleanly.
Topics
Community Discussion
No community discussion yet for this question.