Bird
0
0

You want to keep track of a user's score that changes during the game. Which Kotlin code correctly declares and updates this score using var?

hard📝 Application Q15 of 15
Kotlin - Variables and Type System
You want to keep track of a user's score that changes during the game. Which Kotlin code correctly declares and updates this score using var?
Avar score = 0 score = score + 10 println(score)
Bval score = 0 score = score + 10 println(score)
Cvar score: Int println(score)
Dval score: Int = 0 score += 10 println(score)
Step-by-Step Solution
Solution:
  1. Step 1: Declare a mutable variable with initial value

    Use var score = 0 to allow changes to score.
  2. Step 2: Update the score correctly

    Adding 10 to score with score = score + 10 works because score is mutable.
  3. Final Answer:

    var score = 0 score = score + 10 println(score) -> Option A
  4. Quick Check:

    Mutable var allows updating score [OK]
Quick Trick: Use var for values that change, val for constants [OK]
Common Mistakes:
MISTAKES
  • Using val for variables that need updating
  • Declaring var without initial value and using before assignment
  • Trying to update val variables causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes