Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
iOS Swift - Swift Language Essentials
Identify the error in this Swift code:
var number: Int?
print(number + 5)
ACannot add Int? and Int directly
BMissing semicolon
CVariable number not declared
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand type mismatch

    number is an Optional Int, cannot be added directly to Int without unwrapping.
  2. Step 2: Identify correct error

    Cannot add Int? and Int directly correctly states the problem. Semicolons are optional in Swift. Variable is declared. Code will not run fine.
  3. Final Answer:

    Cannot add Int? and Int directly -> Option A
  4. Quick Check:

    Optional must be unwrapped before arithmetic [OK]
Quick Trick: Unwrap optionals before arithmetic operations [OK]
Common Mistakes:
  • Trying to add optional and non-optional directly
  • Expecting implicit unwrapping
  • Ignoring type mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes