Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' for the value.
Forgetting to use 'to' to create a pair.
✗ Incorrect
The associate function creates a map where each key is the element and the value is its square (it * it).
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'it.length' as key instead of value.
Using a fixed string instead of the variable.
✗ Incorrect
The key should be the word itself, so 'it' is used as the key.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting curly braces around the lambda.
Using it.second instead of it.first for keys.
✗ Incorrect
associateBy expects a lambda expression, so the key selector must be inside curly braces.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toUpperCase()' which is deprecated.
Using word length as value instead of uppercase string.
✗ Incorrect
The key is the word itself (it), and the value is the uppercase form using 'uppercase()' in Kotlin.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'uppercase()' on a Char instead of 'uppercaseChar()'.
Using '<' instead of '>' in filter condition.
✗ Incorrect
Keys are the first letter uppercase (it[0].uppercaseChar()), values are word length (it.length), and filter keeps values greater than 3.