What is the output of the Java code StringBuilder sb = new StringBuilder(“abc”); sb.insert(1, “X”).reverse(); System.out.println(sb);?
Correct answer C. cbXa
Explanation
StringBuilder changes itself in place and returns the same object, which allows chaining. Inserting X at offset 1 turns abc into aXbc, and reversing that gives cbXa. The offset counts from zero and the new text goes before the character at that position. Printing sb calls toString on the modified builder.
Answer checked How we write and check questions
Discussion