Bird
0
0

Identify the error in this Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Collections Fundamentals
Identify the error in this Kotlin code:
val map = mutableMapOf("a" to 1, "b" to 2)
map["c"] = 3
map = mapOf("x" to 10)
ACannot add new key to mutableMapOf
BSyntax error in mapOf creation
CCannot assign a new map to a val variable
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand val variable behavior

    Variables declared with val cannot be reassigned to a new object.
  2. Step 2: Analyze code reassignment

    Code tries to assign a new map to val variable, causing error.
  3. Final Answer:

    Cannot assign a new map to a val variable -> Option C
  4. Quick Check:

    val variables cannot be reassigned = B [OK]
Quick Trick: Use var if you want to reassign a map variable [OK]
Common Mistakes:
MISTAKES
  • Thinking mutableMapOf keys can't be added
  • Ignoring val reassignment rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes