Bird
0
0

Examine the Swift code below and identify the issue related to string value type behavior:

medium📝 Debug Q6 of 15
Swift - Data Types
Examine the Swift code below and identify the issue related to string value type behavior:
var first = "Code"
var second = first
second += " Review"
print(first)
ANo issue; <code>first</code> remains "Code" after modification of <code>second</code>
BModifying <code>second</code> also changes <code>first</code> due to shared reference
CThe code will cause a compile-time error because strings are immutable
DThe print statement will output "Code Review"
Step-by-Step Solution
Solution:
  1. Step 1: Understand string copying

    Assigning first to second copies the string value.
  2. Step 2: Modify second

    Appending to second does not affect first because they are separate copies.
  3. Step 3: Output analysis

    Printing first outputs "Code" as it was not changed.
  4. Final Answer:

    No issue; first remains "Code" after modification of second -> Option A
  5. Quick Check:

    Modifying copy does not affect original string [OK]
Quick Trick: Strings copy on assignment; originals stay unchanged [OK]
Common Mistakes:
  • Assuming strings share references and both change
  • Thinking strings are immutable and cannot be appended

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes