Bird
0
0

Find the mistake in this Swift code:

medium📝 Debug Q7 of 15
Swift - Optionals

Find the mistake in this Swift code:

let a: Int? = 5
let b: Int? = 6
if let a = a, let b = b {
    print(a + b)
} else {
    print("Error")
}
AVariable names in binding shadow original optionals, causing confusion.
BMissing unwrap operator (!) after optionals.
CUsing let instead of var in optional binding.
DNo mistake; code runs correctly.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable shadowing

    Shadowing is allowed and common in optional binding to unwrap values.
  2. Step 2: Check syntax and logic

    Code correctly unwraps optionals and prints sum.
  3. Final Answer:

    No mistake; code runs correctly. -> Option D
  4. Quick Check:

    Shadowing in optional binding is valid [OK]
Quick Trick: Shadowing is normal in optional binding [OK]
Common Mistakes:
  • Thinking shadowing causes errors
  • Expecting forced unwrap needed
  • Confusing let and var usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes