Menu
Question Index
...

What will be the output of the following program?

public class WithoutStringBuffer {
    public static void main(String args[]) {
        String str = "This is my book";
        String reverse1 = new StringBuffer(str).reverse().toString();
        System.out.printf("original String: " + str + ", " + "reversed String: " + reverse1);
    }
    public static String reverse(String str1) {
        String reverse = "";
        for (int i = str1.length() - 1; i >= 0; i--) {
            if (str1.length() > 0)
                reverse = reverse + str1.charAt(i);
            else
                return "";
        }
        return reverse;
    }
}


original String: This is my book, reversed String: koob ym si sihT
original String: This is my book, reversed String: sihT si ym koob
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java StringBuffer, reverse() - Reverse A String In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App