Bird
0
0

Find the problem in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Collections Fundamentals
Find the problem in this Kotlin code snippet:
val pairs = listOf("a" to 1, "b" to 2)
for ((x, y, z) in pairs) {
  println(x + y + z)
}
AToo many variables in destructuring for pair elements
BPairs cannot be iterated with for loop
CVariables x, y, z are not declared
DCannot add string and integers directly
Step-by-Step Solution
Solution:
  1. Step 1: Check destructuring variable count

    Pairs have exactly two components, but code uses three variables.
  2. Step 2: Confirm iteration over list of pairs is valid

    Iterating list of pairs is valid, but destructuring count is wrong.
  3. Final Answer:

    Too many variables in destructuring for pair elements -> Option A
  4. Quick Check:

    Destructuring variables must match element parts [OK]
Quick Trick: Match destructuring variables count to element parts [OK]
Common Mistakes:
MISTAKES
  • Using more variables than pair components
  • Thinking pairs can't be iterated
  • Ignoring variable declaration errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes