Java String Immutability and indexOf Result
Short answer
D Prints 777 because String.replace() returns a new instance and does not modify the original string reference.
Question

Key Takeaways
The common trap is assuming string modification methods change the variable in-place; the insight is that strings are immutable, so the original reference stays the same.
This question tests the immutable nature of Java Strings and how methods like replace() return new instances without modifying the original. The correct answer is D because the index of 'Java' remains unchanged at 7.
Common mistake: Learners often choose option E (71010) by incorrectly assuming the replacement changes the string content or shifts indices, failing to realize 'a' is untouched.
Expert Analysis
Why the Answer Is Correct
The correct answer is D (777) because Java Strings are immutable. When a.replace("Hello!", "Welcome!") is called, it returns a new String object but does not modify the existing variable a. Since a still holds the original value "Hello! Java", a.indexOf("Java") correctly returns 7. Variable b is initialized with the original a, so b.indexOf("Java") also returns 7. Thus, the output is three 7s.
Why the Other Options Are Wrong
Option A (888) and C (7107) assume some shifting or partial replacement occurred. Option B (8109) suggests a complete misinterpretation of indices. Option E (71010) is the most likely distractor, resulting from learners thinking the replacement changed the string structure or that replace modifies the source in place, leading to incorrect index calculations for subsequent operations.
Community Comment Notes
The community overwhelmingly agrees on D, citing immutability as the key factor. As Tojose noted, "the right answer id D. 777, because String is unmutable". Another commenter confirmed, "Print 777. a.replace... doesn’t change a." This consensus highlights a fundamental Java concept frequently tested in certification exams.
Exam Strategy
Always remember that String objects in Java are immutable. Methods like replace, substring, or concat do not change the original string but return a new one. If you don't assign the result back to a variable, the original remains unchanged.
Frequently Asked Questions
Does String.replace() modify the original string?
Why does indexOf return 7 again?
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 8 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 8
-
testostaws 👍 3Print 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.
-
supersquax 👍 2Right 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.
-
Tojose 👍 2the right answer id D. 777, because String is unmutable
-
c6437d5 👍 3 Answered DD tested correct
-
Bharadwaj240796 👍 2Prints : 777, correct answer!
Show 3 more comments
-
meltedolive 👍 2right options D.
-
Uteman 👍 1D is the correct answer
-
xplorerpj 👍 1D is correct answer
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.