Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Optionals
What will be the output of this Swift code?
var age: Int? = nil
print(age!)
ARuntime crash (fatal error)
Bnil
C0
DOptional(0)
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value of the optional

    The variable age is an optional Int set to nil.
  2. Step 2: Understand force unwrapping behavior on nil

    Force unwrapping a nil optional causes a runtime crash (fatal error).
  3. Final Answer:

    Runtime crash (fatal error) -> Option A
  4. Quick Check:

    Force unwrap nil = crash [OK]
Quick Trick: Force unwrapping nil always crashes your program [OK]
Common Mistakes:
  • Expecting nil or 0 output
  • Confusing optional print with unwrapped print
  • Assuming default values on nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes