ProgrammingModerateProduct-company placement · GATE CS

What is printed?

What is printed?
static void m(StringBuilder a, StringBuilder b) {
    a.append("1");
    b = a;
    b.append("2");
}
// in main:
StringBuilder x = new StringBuilder("x"), y = new StringBuilder("y");
m(x, y);
System.out.print(x + " " + y);
  1. A. x1 y2
  2. B. x12 x12
  3. C. x12 y
  4. D. x1 y

Correct answer: C. x12 y

Java passes references by value. Assigning b = a only changes the local copy, so both appends go to x's object (x12) and y is untouched.

More OOP with C++ and Java questions

Exams that ask this topic