Bird
0
0

Find the error in this Swift code:

medium📝 Debug Q7 of 15
Swift - Optionals
Find the error in this Swift code:
var optionalString: String? = nil
let result = optionalString ??
ANo error
BoptionalString cannot be nil
CMissing default value after ??
D?? operator used incorrectly with optional
Step-by-Step Solution
Solution:
  1. Step 1: Check nil coalescing syntax

    The operator ?? must be followed by a default value.
  2. Step 2: Identify missing part

    The code ends abruptly after ?? without a default value, causing a syntax error.
  3. Final Answer:

    Missing default value after ?? -> Option C
  4. Quick Check:

    ?? must have a right side value [OK]
Quick Trick: Always provide a default value after ?? [OK]
Common Mistakes:
  • Leaving ?? without default
  • Assuming optional is unwrapped automatically
  • Syntax errors due to incomplete expression

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes