Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q13 of 15
iOS Swift - Swift Language Essentials
What will be the output of this Swift code?
let number: Int? = nil
if let value = number {
  print("Value is \(value)")
} else {
  print("No value found")
}
ANo value found
Bnil
CValue is 0
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand optional binding

    The code uses if let to safely unwrap the optional number.
  2. Step 2: Check the optional value

    Since number is nil, the else block runs printing "No value found".
  3. Final Answer:

    No value found -> Option A
  4. Quick Check:

    Optional nil triggers else block [OK]
Quick Trick: if let unwraps optionals safely [OK]
Common Mistakes:
  • Assuming nil prints as 0
  • Expecting a runtime crash from nil
  • Thinking code causes compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes