Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Variables and Constants
Identify the error in this Swift code:
let age = 30
age = 31
AVariable 'age' is not initialized
BNo error, code runs fine
CMissing semicolon after first line
DCannot assign to value: 'age' is a 'let' constant
Step-by-Step Solution
Solution:
  1. Step 1: Check variable declaration

    age is declared with let, making it a constant.
  2. Step 2: Analyze assignment

    Trying to assign a new value to a constant causes a compile-time error.
  3. Final Answer:

    Cannot assign to value: 'age' is a 'let' constant -> Option D
  4. Quick Check:

    Assigning to let constant = compile error [OK]
Quick Trick: let constants cannot be reassigned after initialization [OK]
Common Mistakes:
  • Assuming let variables can be reassigned
  • Thinking semicolons are required in Swift
  • Confusing uninitialized variables with constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes