Bird
0
0

Find the issue in this Swift code snippet:

medium📝 Debug Q7 of 15
Swift - Optionals
Find the issue in this Swift code snippet:
var score: Int! = 10
score = nil
print(score + 5)
APrints Optional(15)
BRuntime crash when adding to nil IUO
CPrints 15 without error
DCompile error: cannot assign nil to IUO
Step-by-Step Solution
Solution:
  1. Step 1: Analyze assignment of nil to IUO

    Assigning nil to IUO is allowed but dangerous if accessed later.
  2. Step 2: Accessing IUO after nil assignment

    Adding 5 to score unwraps IUO which is nil, causing runtime crash.
  3. Final Answer:

    Runtime crash when adding to nil IUO -> Option B
  4. Quick Check:

    Using nil IUO in expression = crash [OK]
Quick Trick: IUO can be nil but accessing then crashes [OK]
Common Mistakes:
  • Expecting compile error on nil assignment
  • Assuming safe addition with nil IUO
  • Thinking it prints Optional value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes