Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { StringBuilder sb = new String…

1714Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("ab");
        sb.append("c").reverse();
        sb.insert(1, "X");
        System.out.println(sb);
    }
}
  • AabcX
  • BXcba
  • CcbXa
  • DcXba
Correct answer: D

Explanation

"ab" becomes "abc", reverse() makes it "cba" and insert(1, "X") places X at index 1, giving cXba.

Discussion 0

Nobody has replied yet. Start the discussion!