Bird
0
0

Identify the error in this Kotlin code:

medium📝 Debug Q14 of 15
Kotlin - Basics and JVM Runtime
Identify the error in this Kotlin code:
fun main() {
    val number = 10
    number = 20
    println(number)
}
AMissing semicolon after println
BWrong function name
CCannot reassign a val variable
DVariable type not declared
Step-by-Step Solution
Solution:
  1. Step 1: Understand val vs var in Kotlin

    val declares a read-only variable that cannot be reassigned.
  2. Step 2: Check reassignment in code

    The code tries to assign 20 to number after declaring it as val, causing an error.
  3. Final Answer:

    Cannot reassign a val variable -> Option C
  4. Quick Check:

    val = read-only, no reassignment [OK]
Quick Trick: val means constant, cannot change value later [OK]
Common Mistakes:
MISTAKES
  • Thinking val allows reassignment
  • Assuming semicolons are required
  • Confusing val with var

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes