String str1 = "Kolkata".replace('k', 'a');
In the above statement, the effect on string Kolkata is
String str1 = "Kolkata".replace('k', 'a');
A. The first occurrence of k is replaced by a.
B. All characters k are replaced by a.
C. All characters a are replaced by k.
D. Displays error message
Answer: Option B
Solution(By Examveda Team)
replace() 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:
public String replace(char oldChar, char newChar)
Parameters: Here is the detail of the 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 newChar.
Join The Discussion
Comments ( 1 )
In Java, which class is used to represent a sequence of characters as a string?
A. String
B. StringBuilder
C. StringSequence
D. StringArray
What is the correct way to create a new empty String object in Java?
A. String emptyString = "";
B. String emptyString = new String();
C. String emptyString = " ";
D. String emptyString = null;
E. Both A and B
Which of the following methods is used to compare two strings for equality in Java?
A. equals()
B. compareTo()
C. equalsIgnoreCase()
D. compare()
Will, it also replace "K" with "a"? Because in machine language everything gets converted to ASCII code and the value of "K" and "k" is different with respect to their ASCII code.