Bird
0
0

What is the issue with this Kotlin code snippet?

medium📝 Debug Q6 of 15
Kotlin - Variables and Type System
What is the issue with this Kotlin code snippet?
val score = 100
score += 50
println(score)
AThe variable <code>score</code> is not initialized
BCannot reassign or modify a <code>val</code> variable after initialization
CThe <code>println</code> statement is missing parentheses
DThe operator '+=' is invalid in Kotlin
Step-by-Step Solution
Solution:
  1. Step 1: Understand val immutability

    val variables cannot be reassigned or modified.
  2. Step 2: Analyze the code

    The line score += 50 attempts to modify score, which is illegal.
  3. Final Answer:

    Cannot reassign or modify a val variable after initialization -> Option B
  4. Quick Check:

    val variables are immutable [OK]
Quick Trick: val variables cannot be changed [OK]
Common Mistakes:
MISTAKES
  • Assuming '+=' works on val variables
  • Confusing val with var
  • Ignoring immutability rules in Kotlin

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes