Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q14 of 15
Swift - Variables and Constants
Identify the error in this Swift code:
var name = "Alice"
name = 42
print(name)
ASyntax error on variable declaration
BNo error, prints 42
CType mismatch error when assigning 42 to name
DRuntime error when printing
Step-by-Step Solution
Solution:
  1. Step 1: Check variable type inference

    Variable name is inferred as String because of initial value "Alice".
  2. Step 2: Assign incompatible type

    Assigning an integer 42 to a String variable causes a type mismatch error.
  3. Final Answer:

    Type mismatch error when assigning 42 to name -> Option C
  4. Quick Check:

    Swift variables keep type, cannot assign different type [OK]
Quick Trick: Variable type fixed by first value, cannot assign different type [OK]
Common Mistakes:
  • Ignoring type mismatch errors
  • Thinking var allows any type changes
  • Confusing syntax error with type error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes