Menu
Topics Index
...
`

Strings replacement1121


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings replacement1121
What will be the output of the following program?
public class ReplaceCharacters
{
    public static void main(String args[])
    {
        String str = "Hello Java program.";

        System.out.println(str.replace('H', 'W'));
        System.out.println(str.replaceFirst("He", "Wa"));
        System.out.println(str.replaceAll("He", "Ha"));
    }
}

A. Wello Java program.
Wello Java program.
Wello Java program.
B. Wello Java program.
Wallo Java program.
Hallo Java program.
C. Wello Java program.
Hello Java program.
Hallo Java program.
D. Compilation Error
E. Runtime Error
Topic: Additional String Methods 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 first replace "H" with "W" then "He" with "WA" then "He"  with "Ha"

Posted by Indrasena Reddy    2014-04-15 12:45:45


answer is "C" bcz replace,replaceFirst,replaceAll methods returns new string they do not modify str

Posted by Sai Teja    2014-04-15 12:47:27


ans : a.
because ..h is replaced by w ...so there is no "he" substring ....so wa can't replace anything...likewise ha can't replace anything
so ans is a

Posted by Ananda Kumar    2014-04-15 12:48:11


answer is A because the given string wont contain the patterns given after first print statement.

Posted by Shiva Goud    2014-04-15 13:01:27


answer is C bcz replace,replaceFirst, replaceAll methods returns new string, and there is no string variable 2 hold them

Posted by Pooja Singh    2014-04-15 13:04:14


Answer is B..as first we replace the H with W then we replace the first He present in str with Wa and then we are replacing all the He substrings with Ha and these changes are being done coz we are never changing the str we r jsut  printing the change but not assigning it

Posted by Deepak Kumar Yadav    2014-04-15 13:24:35


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

Posted by Merit Campus    2014-04-16 04:16:48


© meritcampus 2019

All Rights Reserved.

Open In App