Bird
0
0

Identify the error in the following Kotlin code:

medium📝 Debug Q14 of 15
Kotlin - Variables and Type System
Identify the error in the following Kotlin code:
const val greeting = "Hello"
fun main() {
    greeting = "Hi"
    println(greeting)
}
Aconst val must be declared inside a function
BCannot reassign a value to a const val
CMissing semicolon after greeting declaration
DVariable greeting is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Understand const val immutability

    const val declares a constant that cannot be changed after initialization.
  2. Step 2: Analyze the reassignment

    The code tries to assign a new value to greeting which is a constant. This causes a compilation error.
  3. Final Answer:

    Cannot reassign a value to a const val -> Option B
  4. Quick Check:

    const val = fixed, no reassignment allowed [OK]
Quick Trick: const val values cannot be changed after declaration [OK]
Common Mistakes:
MISTAKES
  • Trying to assign new value to const val
  • Thinking semicolon is required in Kotlin
  • Declaring const val inside functions (allowed only at top-level or object)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes