0
0
Kotlinprogramming~10 mins

Associate for map creation 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 create a map using the associate function.

Kotlin
val numbers = listOf(1, 2, 3)
val map = numbers.associate { it to [1] }
Drag options to blanks, or click blank then click option'
Ait - 1
Bit + 1
Cit * it
Dit / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' for the value.
Forgetting to use 'to' to create a pair.
2fill in blank
medium

Complete the code to create a map where keys are strings and values are their lengths.

Kotlin
val words = listOf("apple", "banana", "cherry")
val map = words.associate { [1] to it.length }
Drag options to blanks, or click blank then click option'
Ait.length
Bit
C"word"
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'it.length' as key instead of value.
Using a fixed string instead of the variable.
3fill in blank
hard

Fix the error in the code to correctly create a map from a list of pairs.

Kotlin
val pairs = listOf("a" to 1, "b" to 2)
val map = pairs.associateBy [1]
Drag options to blanks, or click blank then click option'
A{ it.second }
Bit.first
Cit.second
D{ it.first }
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting curly braces around the lambda.
Using it.second instead of it.first for keys.
4fill in blank
hard

Fill both blanks to create a map where keys are words and values are their uppercase forms.

Kotlin
val words = listOf("dog", "cat", "bird")
val map = words.associate { [1] to [2] }
Drag options to blanks, or click blank then click option'
Ait
Bit.toUpperCase()
Cit.uppercase()
Dit.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toUpperCase()' which is deprecated.
Using word length as value instead of uppercase string.
5fill in blank
hard

Fill all three blanks to create a map from a list of words where keys are the first letter uppercase and values are the word length if length > 3.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val map = words.associate { [1] to [2] }
val filteredMap = map.filter { it.value [3] 3 }
Drag options to blanks, or click blank then click option'
Ait[0].uppercaseChar()
Bit.length
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'uppercase()' on a Char instead of 'uppercaseChar()'.
Using '<' instead of '>' in filter condition.