Bird
0
0

Consider this code snippet:

medium📝 Debug Q14 of 15
Swift - Optionals
Consider this code snippet:
var age: Int? = nil
print(age!)

What is the problem and how to fix it?
ANo problem; prints nil safely.
BSyntax error; fix by removing !.
CPrints 0; fix by initializing age to 0.
DRuntime crash due to force unwrapping nil; fix by optional binding.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the cause of crash

    Force unwrapping age! when age is nil causes a runtime crash.
  2. Step 2: Fix using optional binding

    Use if let or guard let to safely unwrap before printing.
  3. Final Answer:

    Runtime crash due to force unwrapping nil; fix by optional binding. -> Option D
  4. Quick Check:

    Force unwrap nil = crash; use safe unwrap [OK]
Quick Trick: Never force unwrap nil; use if let or guard let [OK]
Common Mistakes:
  • Assuming force unwrap prints nil safely
  • Removing ! without handling optional
  • Initializing to 0 without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes