Bird
0
0

What will happen when you run this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Variables and Type System
What will happen when you run this Kotlin code?
val message = "Hello"
message = "World"
println(message)
APrints: Hello
BPrints: World
CCompilation error: Val cannot be reassigned
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand val reassignment

    Variables declared with val cannot be reassigned after initialization.
  2. Step 2: Reassignment attempt

    Trying to assign "World" to message causes a compilation error.
  3. Final Answer:

    The code will not compile and gives a compilation error -> Option C
  4. Quick Check:

    val variables are immutable references [OK]
Quick Trick: val variables cannot be reassigned [OK]
Common Mistakes:
MISTAKES
  • Assuming val variables can be reassigned
  • Expecting runtime error instead of compile error
  • Confusing val with var

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes