ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
StringBuilder[] sbs = new StringBuilder[3];
Arrays.fill(sbs, new StringBuilder());
sbs[0].append("x");
System.out.print(sbs[2] + " " + (sbs[0] == sbs[1]));
  1. A. (empty) false
  2. B. null false
  3. C. x true
  4. D. x false

Correct answer: C. x true

Arrays.fill stores the same reference in every slot; only one StringBuilder is ever created, so appending through sbs[0] is visible through sbs[2].

More OOP with C++ and Java questions

Exams that ask this topic