Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Variables and Type System
What will be the output of this Kotlin code?
val x = 10
// x = 20
println(x)
A20
BRuntime error
C10
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code behavior

    The variable x is declared with val, so it is immutable and cannot be reassigned.
  2. Step 2: Analyze the commented reassignment

    The reassignment x = 20 is commented out, so it does not run. The value of x remains 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Immutable val keeps value 10 [OK]
Quick Trick: Commented code does not run, so value stays same [OK]
Common Mistakes:
MISTAKES
  • Assuming commented code runs
  • Thinking val variables can be reassigned
  • Confusing compilation and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes