Bird
0
0

Given nested optionals let data: Int?? = 5, which expression safely unwraps to get the Int value?

hard📝 Application Q9 of 15
Swift - Operators and Expressions
Given nested optionals let data: Int?? = 5, which expression safely unwraps to get the Int value?
Adata ?? 0
Bdata! ?? 0
Cdata?? ?? 0
Ddata?.flatMap { $0 } ?? 0
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested optionals

    Int?? means optional of optional Int, so unwrapping requires two steps.
  2. Step 2: Use flatMap to unwrap inner optional safely

    data?.flatMap { $0 } unwraps outer optional and then inner optional safely.
  3. Step 3: Provide default with ??

    If any unwrap fails, ?? 0 returns 0.
  4. Final Answer:

    data?.flatMap { $0 } ?? 0 -> Option D
  5. Quick Check:

    Nested optional unwrap needs flatMap + ?? [OK]
Quick Trick: Use flatMap to unwrap nested optionals safely [OK]
Common Mistakes:
  • Using ?? once only unwraps outer optional
  • Using ! causes crash if nil
  • Incorrect syntax with double ?? operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes