Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q6 of 15
Swift - Optionals
Identify the error in this Swift code snippet:
var score: Int? = 10
print(score * 2)
AMissing semicolon at the end of print statement
Bscore should be declared as a constant
CCannot use * operator directly on an optional without unwrapping
DOptional variables cannot hold integers
Step-by-Step Solution
Solution:
  1. Step 1: Analyze operation on optional

    score is an optional Int, so arithmetic operators cannot be applied directly.
  2. Step 2: Correct usage

    score must be unwrapped safely before multiplying, e.g., using if let or !.
  3. Final Answer:

    Cannot use * operator directly on an optional without unwrapping -> Option C
  4. Quick Check:

    Optionals need unwrapping before arithmetic = C [OK]
Quick Trick: Unwrap optionals before using operators [OK]
Common Mistakes:
  • Trying to multiply optional directly
  • Thinking semicolon is required
  • Believing optionals can't hold Int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes