Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { String s = "hello"; s.concat(…

1136Medium 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) {
        String s = "hello";
        s.concat(" world");
        s.toUpperCase();
        StringBuilder sb = new StringBuilder("ab");
        sb.append("cd").reverse();
        System.out.println(s + " " + sb);
    }
}
  • AHELLO WORLD
  • Bhello dcba
  • Chello abcd
  • Dhello world dcba
Correct answer: B

Explanation

Strings are immutable so s is unchanged ("hello"); the StringBuilder is modified in place to "abcd" and then reversed to "dcba".

Discussion 0

Nobody has replied yet. Start the discussion!