Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of following program?
public class Replace {
    public static void main(String args[]) {
        String str = "This replace method of String will return a new String object if there is a replace.";
        String replacestr = str.replace('r', 's');

A. This seplace method of Stsing will setusn 
a new Stsing object if these is a seplace.
B. This seplace method of String will return 
a new String object if there is a replace.
C. This replace method of String will return 
a new String object if there is a seplace.
D. Compilation Error or Runtime Error
Topic: replace() Method 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.

Answer is a...replace method in the program will replace every occurence of 'r' with 's' in the string str and the resultant string is assigned to replacestr and is printed to console

Posted by Shashanka Mogaliraju    2014-12-31 04:29:28

ans is a. here the string class is used which is described as TheStringclass represents character strings. Allstring literals in Java programs, such as"abc", are implemented as instances of this class..... now replace()-->>Description:This method returns a new string resulting from replacing all occurrences of oldChar in this string with newChar......Syntax:-Here is the syntax of this method:publicString replace(char oldChar,char newChar).......Parameter:-Here is the detail of parameters:-oldChar-- the old character......newChar-- the new character.......Return Value:-It returns a string derived from this string by replacing every occurrence of oldChar with here the function replace will replace all the occirences of r in the string with s,.......and as the result stored in replacestr hence printed

Posted by Asma Mujtaba Khan    2014-12-31 04:48:24

The str.replace will replace all the words in the sentence it will if we print only 'str' it will print with out replacing as it is... And if we print replacestr it will print replaced words
So here we are printing only replace str so it will print the replaced once

Posted by Fuzail Ahmed    2014-12-31 04:51:14

ans a
In Java, String ‘replace’ and its variants are used to replace a character, string or a regular expression from a String object with a character or string replacement. When there is a change in the String object while performing ‘replace’, a new String is returned whereas in a case of no change, the same object is returned.

Java String Replace Method Syntax

String replace (char charToReplace, char replacementChar)

In the above “replace” method format, each occurrence of a character ‘charToReplace’ in a string is replaced with the character ‘replacementChar’ and a new String object containing this character sequence is returned.

coming to this program
string object is initialized with "This replace method of String will return a new String object if there is a replace."

thn we assigning "r","s" replaced string to replacestr.

here we are storing the changed string value.

but str value remains same.

sop prints "This seplace method of Stsing will setusn
a new Stsing object if these is a seplace."
this string get printed

Posted by Maheshwari Natarajan    2014-12-31 12:04:32

This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Asma Mujtaba Khan, Fuzail Ahmed,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-03 03:27:05

© meritcampus 2019

All Rights Reserved.

Open In App