Bird
0
0

Find the mistake in this Swift code snippet:

medium📝 Debug Q7 of 15
Swift - Data Types
Find the mistake in this Swift code snippet:
var s1 = "Hello"
var s2 = &s1
s2 += " World"
print(s1)
AAppending to s2 is not allowed
BUsing & to assign a string reference is invalid
Cs1 is not declared
Dprint statement syntax is wrong
Step-by-Step Solution
Solution:
  1. Step 1: Understand & usage in Swift

    & is used for inout parameters, not for assigning references to variables.
  2. Step 2: Analyze assignment

    Assigning &s1 to s2 is invalid syntax for strings in Swift.
  3. Final Answer:

    Using & to assign a string reference is invalid -> Option B
  4. Quick Check:

    No pointer references for strings in Swift [OK]
Quick Trick: No pointer references for strings in Swift [OK]
Common Mistakes:
  • Using & to get reference
  • Expecting reference semantics
  • Confusing with C pointers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes