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:
let name = "Alice"
name = "Bob"
ACannot assign to 'name' because it is a constant
BMissing semicolon after first line
CVariable 'name' not declared
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check declaration of name

    name is declared with let, making it a constant.
  2. Step 2: Check reassignment attempt

    The code tries to assign a new value to name, which is not allowed for constants.
  3. Final Answer:

    Cannot assign to 'name' because it is a constant -> Option A
  4. Quick Check:

    let constants cannot be reassigned [OK]
Quick Trick: let means no change allowed, reassignment causes error [OK]
Common Mistakes:
  • Assuming let variables can be reassigned
  • Thinking semicolon is required in Swift
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes