Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 19

Given the code fragment:

What is the result?

    Correct Answer: D

    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.

Discussion
c6437d5Option: D

D tested correct

testostawsOption: D

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.

Bharadwaj240796Option: D

Prints : 777, correct answer!

supersquaxOption: D

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.

meltedoliveOption: D

right options D.

TojoseOption: D

the right answer id D. 777, because String is unmutable

xplorerpjOption: D

D is correct answer

UtemanOption: D

D is the correct answer