What is destructuring in Kotlin collection iteration?
Destructuring lets you split an object into multiple variables directly in a loop, making it easy to work with pairs or data classes inside collections.
Click to reveal answer
beginner
How do you destructure a Pair in a Kotlin for-loop?
Use syntax like for ((a, b) in pairs) { ... } where a and b get values from each Pair's first and second elements.
Click to reveal answer
intermediate
Can you destructure a data class in a Kotlin collection iteration?
Yes! If a data class has component functions, you can destructure it in a loop like <code>for ((x, y) in dataList) { ... }</code> to get properties directly.
Click to reveal answer
intermediate
What happens if you try to destructure a collection element without component functions?
You get a compile error because Kotlin needs component functions to split an object into parts during destructuring.
Click to reveal answer
beginner
Why is destructuring useful in real-life Kotlin coding?
It makes code cleaner and easier to read by directly extracting needed values from complex objects during iteration, like keys and values from maps.
Click to reveal answer
Which syntax correctly destructures a Pair in a Kotlin for-loop?
Afor (a in pairs) { val b = a.second }
Bfor (a, b in pairs) { ... }
Cfor (pair in pairs) { val (a, b) = pair }
Dfor ((a, b) in pairs) { ... }
✗ Incorrect
Option D uses the correct destructuring syntax directly in the loop declaration.
What must a class have to be destructured in Kotlin?
AA toString() method
BComponent functions like component1(), component2()
CA constructor with parameters
DAn equals() method
✗ Incorrect
Destructuring requires component functions to extract parts of the object.
Which Kotlin collection type is commonly destructured into key and value?
AMap
BList
CSet
DArray
✗ Incorrect
Maps have entries that can be destructured into key and value pairs.
What happens if you try to destructure a class without component functions?
AYou get a compile-time error
BIt works fine
CIt returns null values
DIt throws a runtime exception
✗ Incorrect
Kotlin requires component functions for destructuring; otherwise, it won't compile.
Which of these is a benefit of destructuring in iteration?
AMakes code longer
BSlows down the program
CImproves code readability
DRemoves type safety
✗ Incorrect
Destructuring improves readability by extracting values directly.
Explain how destructuring works in Kotlin when iterating over a collection of Pairs.
Think about how you can split each Pair into two variables directly in the loop.
You got /3 concepts.
Describe what requirements a class must meet to support destructuring in Kotlin.
Consider what Kotlin needs to split an object into parts.
You got /3 concepts.
Practice
(1/5)
1. What does destructuring in Kotlin collection iteration allow you to do? for ((key, value) in map) { ... }
easy
A. Only access the value in the loop
B. Only access the key in the loop
C. Access both key and value directly in the loop variables
D. Modify the map while iterating
Solution
Step 1: Understand destructuring syntax
In Kotlin, for ((key, value) in map) extracts both key and value from each map entry.
Step 2: Recognize loop variable usage
This lets you use key and value directly inside the loop without extra calls.
Final Answer:
Access both key and value directly in the loop variables -> Option C
Quick Check:
Destructuring = direct access to parts [OK]
Hint: Destructuring splits pairs into variables directly [OK]
Common Mistakes:
Thinking only key or value is accessible
Trying to modify map inside loop
Not using parentheses for destructuring
2. Which of the following is the correct syntax for destructuring a map in Kotlin? val map = mapOf("a" to 1, "b" to 2)
easy
A. for (key, value in map) { println(key + value) }
B. for ((key, value) in map) { println(key + value) }
C. for [key, value] in map { println(key + value) }
D. for {key, value} in map { println(key + value) }
Solution
Step 1: Recall Kotlin destructuring syntax
Kotlin requires parentheses around variables when destructuring: for ((key, value) in map).
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.
Final Answer:
for ((key, value) in map) { println(key + value) } -> Option B
Quick Check:
Parentheses needed for destructuring [OK]
Hint: Use parentheses around variables in destructuring [OK]
Common Mistakes:
Omitting parentheses in for loop
Using square or curly brackets instead
Confusing destructuring with tuple unpacking syntax
3. What is the output of this Kotlin code?
val map = mapOf("x" to 10, "y" to 20)
for ((k, v) in map) {
println("$k$v")
}
medium
A. Syntax error
B. 10x\n20y
C. k v\nk v
D. x10\ny20
Solution
Step 1: Understand map entries and destructuring
The map has entries "x" to 10 and "y" to 20. The loop destructures each entry into k and v.
Step 2: Print key and value concatenated
Inside the loop, println("$k$v") prints key and value together without space, so outputs are "x10" and "y20" on separate lines.
Final Answer:
x10\ny20 -> Option D
Quick Check:
Print key + value per entry [OK]
Hint: Destructure map entries to print key and value [OK]
Common Mistakes:
Swapping key and value in output
Expecting spaces between key and value
Confusing variable names
4. 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)
}
medium
A. Missing parentheses around key and value in for loop
B. mapOf cannot be iterated
C. Cannot add key and value directly
D. No error, code runs fine
Solution
Step 1: Check for destructuring syntax
The for loop uses for (key, value in map) which lacks parentheses around key, value.
Step 2: Understand correct syntax
Kotlin requires parentheses for destructuring pairs: for ((key, value) in map). Without them, it's a syntax error.
Final Answer:
Missing parentheses around key and value in for loop -> Option A
Quick Check:
Parentheses required for destructuring [OK]
Hint: Always use parentheses for destructuring in for loops [OK]
Common Mistakes:
Omitting parentheses in destructuring
Thinking mapOf is not iterable
Confusing addition with string concatenation
5. Given a list of pairs representing database rows:
val rows = listOf(Pair("id", 1), Pair("name", "Alice"), Pair("age", 30))
Which Kotlin code correctly prints each key and value using destructuring?
hard
A. for ((key, value) in rows) { println("$key: $value") }
B. for (key, value in rows) { println("$key: $value") }
C. for (row in rows) { println(row.key + ": " + row.value) }
D. for ((k, v) in rows) { println(k + v) }
Solution
Step 1: Recognize list of pairs and destructuring
The list contains pairs, so destructuring with (key, value) works to access each part.
Step 2: Check each option for correctness
for ((key, value) in rows) { println("$key: $value") } uses correct destructuring syntax and prints key and value with colon and space. for (key, value in rows) { println("$key: $value") } misses parentheses, for (row in rows) { println(row.key + ": " + row.value) } tries to access properties that don't exist, for ((k, v) in rows) { println(k + v) } concatenates without separator.
Final Answer:
for ((key, value) in rows) { println("$key: $value") } -> Option A
Quick Check:
Use parentheses and print with separator [OK]
Hint: Use parentheses and print key: value format [OK]
Common Mistakes:
Omitting parentheses in for loop
Accessing pair elements as properties
Concatenating without separator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.