Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Optionals
What will be printed by this Swift code?
let optionalNumber: Int? = 5
let number = optionalNumber ?? 10
print(number)
A10
B5
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Check optionalNumber value

    optionalNumber contains 5, so it is not nil.
  2. Step 2: Apply nil coalescing operator

    Since optionalNumber is not nil, the operator returns the left side value 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Nil coalescing returns left side if not nil [OK]
Quick Trick: Returns left value if not nil, else right [OK]
Common Mistakes:
  • Assuming default is always returned
  • Confusing left and right sides
  • Expecting nil output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes