Bird
0
0

In Swift, what happens when you assign one String variable to another and then change the new variable?

easy📝 Conceptual Q11 of 15
Swift - Data Types
In Swift, what happens when you assign one String variable to another and then change the new variable?
AThe original string remains unchanged because strings are value types.
BThe original string also changes because strings are reference types.
CBoth strings become empty.
DThe program crashes due to memory error.
Step-by-Step Solution
Solution:
  1. Step 1: Understand String assignment in Swift

    Strings in Swift are value types, so assigning one string to another copies the value.
  2. Step 2: Effect of modifying the copied string

    Changing the copied string does not affect the original because they are separate copies.
  3. Final Answer:

    The original string remains unchanged because strings are value types. -> Option A
  4. Quick Check:

    String assignment copies value = original unchanged [OK]
Quick Trick: Remember: Swift strings copy on assignment, no shared changes [OK]
Common Mistakes:
  • Thinking strings are reference types and share changes
  • Assuming original string changes after modifying copy
  • Confusing strings with classes or objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes