Bird
0
0

Which of these expressions correctly uses the nil coalescing operator to provide a default value of 10 when optionalInt is nil?

easy📝 Conceptual Q2 of 15
Swift - Optionals
Which of these expressions correctly uses the nil coalescing operator to provide a default value of 10 when optionalInt is nil?
Alet value = optionalInt ?: 10
Blet value = 10 ?? optionalInt
Clet value = optionalInt ? 10
Dlet value = optionalInt ?? 10
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct nil coalescing syntax

    The operator is used as optionalValue ?? defaultValue.
  2. Step 2: Check each option

    let value = optionalInt ?? 10 matches the correct syntax; others are invalid or incorrect.
  3. Final Answer:

    let value = optionalInt ?? 10 -> Option D
  4. Quick Check:

    Correct nil coalescing syntax [OK]
Quick Trick: Use optional ?? default to provide fallback [OK]
Common Mistakes:
  • Swapping operands
  • Using ternary operator syntax
  • Using ?: which is not Swift

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes