Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to destructure each pair in the list and print the key.
Kotlin
val pairs = listOf("a" to 1, "b" to 2) for (([1], value) in pairs) { println([1]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' or 'item' which are not the default names for Pair components.
✗ Incorrect
In Kotlin, when destructuring a Pair, the first element is commonly named 'first'.
2fill in blank
mediumComplete the code to destructure map entries and print both key and value.
Kotlin
val map = mapOf("x" to 10, "y" to 20) for (([1], value) in map) { println("Key: $[1], Value: $value") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'entry' or 'index' which are not valid destructuring names for map entries.
✗ Incorrect
When iterating over a map, destructuring uses 'key' and 'value' to access entries.
3fill in blank
hardFix the error in destructuring a list of triples to print all three elements.
Kotlin
val triples = listOf(Triple(1, "a", true), Triple(2, "b", false)) for ((first, [1], third) in triples) { println("$first, $[1], $third") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'middle' or 'value' which are not valid component names for Triple.
✗ Incorrect
Triple destructuring uses 'first', 'second', and 'third' as component names.
4fill in blank
hardFill both blanks to destructure a list of pairs and print their sum.
Kotlin
val pairs = listOf(1 to 2, 3 to 4) for (([1], [2]) in pairs) { println([1] + [2]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' and 'value' which are for map entries, not pairs.
✗ Incorrect
Pairs are destructured into 'first' and 'second' components in Kotlin.
5fill in blank
hardFill all three blanks to destructure a list of triples and print a formatted string.
Kotlin
val triples = listOf(Triple("John", "Doe", 30), Triple("Jane", "Smith", 25)) for (([1], [2], [3]) in triples) { println("Name: $[1] $[2], Age: $[3]") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last' instead of 'second' for the middle element.
✗ Incorrect
Triple destructuring uses 'first', 'second', and 'third' to access elements in order.