Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.