Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The code initializes a string 'a' with the value 'Hello! Java'. The first call to a.indexOf('Java') returns 7 because 'Java' starts at index 7 in the string. The a.replace('Hello!', 'Welcome!') method does not change 'a' itself because strings are immutable in Java; it returns a new string, which is not assigned to any variable. Therefore, the next call to a.indexOf('Java') still returns 7. The StringBuilder 'b' is created with the contents of 'a', so b.indexOf('Java') also returns 7. Hence, the output is 777.
D tested correct
Print 777. a.replace("Hello!", "Welcome!") doesn’t change a. It creates a new string which is not used. Therefore, a.indexOf("Java") will return the same index (7) both times. The same index will be returned by b.indexOf("Java") since b is created from a.
Prints : 777, correct answer!
Right one is D. As Tojose writes, Strings are immutable. When doing a.replace(), it returns a new string which in this case isnt saved anywhere, and so "a" remains the same.
right options D.
the right answer id D. 777, because String is unmutable
D is correct answer
D is the correct answer