Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q13 of 15
iOS Swift - Swift Language Essentials
What will be printed by this Swift code?
var score: Int? = 85
if let safeScore = score {
  print("Score is \(safeScore)")
} else {
  print("No score")
}
AScore is 85
BNo score
COptional(85)
DError: score is Optional
Step-by-Step Solution
Solution:
  1. Step 1: Understand Optional value

    Variable score holds Optional Int with value 85.
  2. Step 2: Analyze if let unwrapping

    The if let safeScore = score unwraps the value 85 successfully, so the print inside if runs.
  3. Final Answer:

    Score is 85 -> Option A
  4. Quick Check:

    Unwrapped Optional prints value, not nil or error [OK]
Quick Trick: If Optional has value, if let unwrap runs [OK]
Common Mistakes:
  • Expecting Optional(...) string output
  • Confusing else block execution
  • Thinking unwrapping causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes