Bird
0
0

Identify the issue in this Kotlin code snippet:

medium📝 Debug Q6 of 15
Kotlin - Collections Fundamentals
Identify the issue in this Kotlin code snippet:
val map = mapOf("x" to 10)
for (x, y in map) {
  println(x + y)
}
AThe map is empty, so loop won't execute.
BUsing immutable map prevents iteration.
CThe '+' operator cannot be used with strings and integers.
DMissing parentheses around variables in the for loop destructuring.
Step-by-Step Solution
Solution:
  1. Step 1: Check loop syntax

    Destructuring requires parentheses: for ((x, y) in map).
  2. Step 2: Identify error

    Code uses for (x, y in map) which is invalid syntax.
  3. Final Answer:

    Missing parentheses around variables in the for loop destructuring. -> Option D
  4. Quick Check:

    Parentheses are mandatory for destructuring in loops [OK]
Quick Trick: Always use parentheses for destructuring in for loops [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in destructuring declaration
  • Confusing map immutability with syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes