nerdexam
Oracle

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…

Java Object-Oriented Approach

Question

public class Test { private String strings; } Which two construct ors will compile and set the class field strings? (Choose two.)

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)
  • A
    4% (2)
  • B
    6% (3)
  • C
    79% (37)
  • D
    11% (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 with String[] - you'd need toArray() 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 String scalar, which cannot be assigned to a String[] 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

#constructors#varargs#type compatibility#method parameters

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice