Java String Immutability and indexOf Result

1Z0-829 By islever Editorial Last updated

Short answer

D Prints 777 because String.replace() returns a new instance and does not modify the original string reference.

Question

Given the code fragment: What is the result? - Exhibit: Java String Immutability and indexOf Result
  1. 888
  2. 8109
  3. 7107
  4. 777 Correct Answer
  5. 71010

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?

No, Strings are immutable. replace() returns a new string; the original variable remains unchanged unless reassigned.

Why does indexOf return 7 again?

Because the variable 'a' was never updated with the result of replace(), so it still points to the original string containing 'Java' at index 7.

More 1Z0-829 FAQ →

Community Votes

D
100%

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

  1. testostaws 👍 3
    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.
  2. supersquax 👍 2
    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.
  3. Tojose 👍 2
    the right answer id D. 777, because String is unmutable
  4. c6437d5 👍 3 Answered D
    D tested correct
  5. Bharadwaj240796 👍 2
    Prints : 777, correct answer!
Show 3 more comments
  1. meltedolive 👍 2
    right options D.
  2. Uteman 👍 1
    D is the correct answer
  3. xplorerpj 👍 1
    D is correct answer

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to 1Z0-829 Study Guide