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: String = 123
print(name)
AType mismatch: cannot assign Int to String
BVariable name is a reserved keyword
CMissing explicit type annotation
DNo error, prints 123
Step-by-Step Solution
Solution:
  1. Step 1: Check the declared type and assigned value

    The variable name is declared as String but assigned an integer 123, which is a type mismatch.
  2. Step 2: Understand Swift's type safety

    Swift does not allow assigning an Int to a String variable, causing a compile-time error.
  3. Final Answer:

    Type mismatch: cannot assign Int to String -> Option A
  4. Quick Check:

    Type mismatch error = Type mismatch: cannot assign Int to String [OK]
Quick Trick: Check if assigned value matches declared type exactly [OK]
Common Mistakes:
  • Assuming implicit conversion from Int to String
  • Ignoring type mismatch errors
  • Thinking variable names are reserved keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes