Bird
0
0

Identify the error in this Swift REPL snippet:

medium📝 Debug Q7 of 15
Swift - Basics and Runtime
Identify the error in this Swift REPL snippet:
var message = "Welcome"
print(message - "Home")
AVariable 'message' is immutable
BCannot subtract strings using '-' operator
CMissing semicolon at the end
Dprint function is used incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Review the code

    Variable message holds a string "Welcome".
  2. Step 2: Check the operation

    The code attempts to subtract one string from another using '-'.
  3. Step 3: Understand Swift string operations

    Swift does not support subtraction between strings.
  4. Final Answer:

    Cannot subtract strings using '-' operator -> Option B
  5. Quick Check:

    Strings can be concatenated but not subtracted [OK]
Quick Trick: Strings cannot be subtracted with '-' [OK]
Common Mistakes:
  • Assuming '-' works like concatenation
  • Thinking variable immutability causes error
  • Believing print syntax is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes