Menu
Topics Index
...
`

Strings1634


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings1634
What will be the output of the following program?
public class DemoOnSubStrings {
    public static void main(String args[]) {
        String input = "hello";
        String search = "hel";
        String subString = "count";
        String result = "";
        int i = input.length();
        System.out.println(input);
        for (int j = 0; j < i; j++) {
            result = input.substring(0, j);
            result = result + subString;
            result = result + input.substring(j + search.length());
            input = result;
        }
        System.out.println(input);
    }
}

A. hello
cccountttttlo
B. hello
cccccountttttlo
C. hello
cccountttlo
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java String substring() method - substring In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans B subString(0,0) will not print any value by appending it will print the B

Posted by Bheema Reddy    2014-07-21 08:49:43


ans is B,here for loop iterates 5 times bcoz input string ie hello length is 5 and in each iteration it find s input.substring(0,j) +subString+input.substring(j+search.length()) and assigns to input var.

Posted by Raviteja Daggupati    2014-07-21 08:57:12


B

Posted by Mohd Naveed    2014-07-21 10:11:16


b

Posted by Vino Krishnan    2014-07-21 14:15:00


B

Posted by Haseeb Baig    2014-07-21 17:52:44


Congratulations Bheema Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-22 04:27:03


b

Posted by Patil Ranjit    2014-07-22 05:34:02


© meritcampus 2019

All Rights Reserved.

Open In App