Bird
0
0

Which of the following is the correct syntax for destructuring a map in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Collections Fundamentals
Which of the following is the correct syntax for destructuring a map in Kotlin?
val map = mapOf("a" to 1, "b" to 2)
Afor (key, value in map) { println(key + value) }
Bfor ((key, value) in map) { println(key + value) }
Cfor [key, value] in map { println(key + value) }
Dfor {key, value} in map { println(key + value) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin destructuring syntax

    Kotlin requires parentheses around variables when destructuring: for ((key, value) in map).
  2. Step 2: Check each option

    Only for ((key, value) in map) { println(key + value) } uses parentheses correctly; others use commas without parentheses or wrong brackets.
  3. Final Answer:

    for ((key, value) in map) { println(key + value) } -> Option B
  4. Quick Check:

    Parentheses needed for destructuring [OK]
Quick Trick: Use parentheses around variables in destructuring [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in for loop
  • Using square or curly brackets instead
  • Confusing destructuring with tuple unpacking syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes