ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
StringBuilder sb = new StringBuilder("abcdef");
sb.insert(2, "XY").delete(5, 7).reverse().setCharAt(0, 'Z');
System.out.print(sb);
  1. A. ZYXba
  2. B. ZcYXba
  3. C. ZcXYba
  4. D. ZecYXba

Correct answer: B. ZcYXba

insert gives abXYcdef; delete(5, 7) removes indices 5 and 6 (d, e) → abXYcf; reverse → fcYXba; setCharAt(0, 'Z') → ZcYXba. All of these mutate the same builder.

More OOP with C++ and Java questions

Exams that ask this topic