Bird
0
0

Given String s = "Hello"; and String t = s;, what happens if you do t = t + " World";? What is the value of s afterwards?

hard📝 Application Q9 of 15
Java - Strings and String Handling
Given String s = "Hello"; and String t = s;, what happens if you do t = t + " World";? What is the value of s afterwards?
Anull
B"Hello World"
C"Hello"
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand string immutability and assignment

    Strings cannot be changed after creation. t = t + " World" creates a new string and assigns it to t.
  2. Step 2: Check original variable s

    s still points to original "Hello" string because strings are immutable and assignment to t does not affect s.
  3. Final Answer:

    "Hello" -> Option C
  4. Quick Check:

    Strings are immutable; original stays same [OK]
Quick Trick: String variables hold references; original string unchanged [OK]
Common Mistakes:
  • Assuming s changes when t changes
  • Confusing references with values
  • Expecting mutable strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes