Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Collections Fundamentals
Identify the error in this Kotlin code snippet:
val map = mapOf("a" to 1, "b" to 2)
for (key, value in map) {
  println(key + value)
}
AMissing parentheses around key and value in for loop
BmapOf cannot be iterated
CCannot add key and value directly
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check for destructuring syntax

    The for loop uses for (key, value in map) which lacks parentheses around key, value.
  2. Step 2: Understand correct syntax

    Kotlin requires parentheses for destructuring pairs: for ((key, value) in map). Without them, it's a syntax error.
  3. Final Answer:

    Missing parentheses around key and value in for loop -> Option A
  4. Quick Check:

    Parentheses required for destructuring [OK]
Quick Trick: Always use parentheses for destructuring in for loops [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in destructuring
  • Thinking mapOf is not iterable
  • Confusing addition with string concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes