Bird
0
0

What does the nil coalescing operator ?? do in Swift?

easy📝 Conceptual Q11 of 15
Swift - Optionals
What does the nil coalescing operator ?? do in Swift?
AIt unwraps an optional without checking for nil.
BIt provides a default value if an optional is nil.
CIt converts a non-optional to an optional.
DIt forces a runtime error if the optional is nil.
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional values

    Optionals may contain a value or nil (no value).
  2. Step 2: Role of nil coalescing operator

    The operator ?? returns the optional's value if it exists; otherwise, it returns the default value provided.
  3. Final Answer:

    It provides a default value if an optional is nil. -> Option B
  4. Quick Check:

    Nil coalescing = default value if nil [OK]
Quick Trick: Remember: ?? means "use this if nil" [OK]
Common Mistakes:
  • Thinking ?? unwraps without nil check
  • Confusing ?? with forced unwrapping (!)
  • Assuming ?? converts types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes