Discussion Forum : Searching And Modifying A String
Question -


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}
Options:
A .  hello
B .  helwo
C .  hewlo
D .  hewwo
Answer: Option D

replace() method replaces all occurrences of one character in invoking string with another character. s1.replace('l','w') replaces every occurrence of 'l' in hello by 'w', giving hewwo.
Output:
$ javac output.java
$ java output
hewwo



Was this answer helpful ?
Next Question
Submit Your Solution hear:

Your email address will not be published. Required fields are marked *