Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q14 of 15
Swift - Optionals
Identify the error in this Swift code snippet:
var age: Int?
age = nil
print(age!)
ASyntax error: missing initialization
BNo error, prints nil
CRuntime error: force unwrapping nil
DCompile error: optional not declared
Step-by-Step Solution
Solution:
  1. Step 1: Understand force unwrapping

    Using ! on an optional tries to get its value directly.
  2. Step 2: Check the value of age

    Since age is nil, force unwrapping causes a runtime crash.
  3. Final Answer:

    Runtime error: force unwrapping nil -> Option C
  4. Quick Check:

    Force unwrap nil causes runtime error [OK]
Quick Trick: Never force unwrap optionals without checking nil first [OK]
Common Mistakes:
  • Assuming force unwrap prints nil
  • Ignoring runtime crash possibility
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes