Bird
0
0

You want to track a user's score that changes during gameplay. Which Swift code snippet correctly declares and updates this score?

hard📝 Application Q8 of 15
Swift - Variables and Constants
You want to track a user's score that changes during gameplay. Which Swift code snippet correctly declares and updates this score?
Avar score: String = "0" score = 5
Blet score = 0 score = score + 5
Cscore var = 0 score = 5
Dvar score = 0 score += 5
Step-by-Step Solution
Solution:
  1. Step 1: Mutable variable declaration

    Use var to declare score so it can be changed.
  2. Step 2: Update score correctly

    score += 5 increments the score by 5.
  3. Step 3: Identify incorrect options

    • let score = 0 score = score + 5 uses let, which is immutable.
    • score var = 0 score = 5 has invalid syntax.
    • var score: String = "0" score = 5 declares score as String but assigns Int later, causing type error.
  4. Final Answer:

    var score = 0 score += 5 -> Option D
  5. Quick Check:

    Use var for mutable variables and proper type [OK]
Quick Trick: Declare with var and update with += for mutable values [OK]
Common Mistakes:
  • Using let for variables that change
  • Incorrect syntax for variable declaration
  • Type mismatch between declared and assigned values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes