0
0
Kotlinprogramming~10 mins

Destructuring in collection iteration in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Akey
Bfirst
Citem
Delement
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' or 'item' which are not the default names for Pair components.
2fill in blank
medium

Complete 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'
Aentry
Belement
Cindex
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'entry' or 'index' which are not valid destructuring names for map entries.
3fill in blank
hard

Fix 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'
Asecond
Bmiddle
Cvalue
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'middle' or 'value' which are not valid component names for Triple.
4fill in blank
hard

Fill 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'
Afirst
Bsecond
Ckey
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' and 'value' which are for map entries, not pairs.
5fill in blank
hard

Fill 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'
Afirst
Blast
Cthird
Dsecond
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last' instead of 'second' for the middle element.