0
0
Kotlinprogramming~20 mins

Associate for map creation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Associate Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using associate?
Consider the following Kotlin code snippet that creates a map from a list of strings using associate. What will be printed?
Kotlin
val fruits = listOf("apple", "banana", "cherry")
val map = fruits.associate { it to it.length }
println(map)
ACompilation error
B{apple=5, banana=6, cherry=7}
C{apple=4, banana=6, cherry=6}
D{apple=5, banana=6, cherry=6}
Attempts:
2 left
💡 Hint
Remember that associate creates a map where each key-value pair is defined by the lambda.
Predict Output
intermediate
2:00remaining
What does this Kotlin code print when using associateBy?
Look at this Kotlin code that uses associateBy. What is the output?
Kotlin
data class Person(val name: String, val age: Int)

val people = listOf(Person("Alice", 30), Person("Bob", 25), Person("Charlie", 30))
val map = people.associateBy { it.age }
println(map)
A{30=Person(name=Alice, age=30), 25=Person(name=Charlie, age=30)}
B{30=Person(name=Alice, age=30), 25=Person(name=Bob, age=25)}
C{30=Person(name=Charlie, age=30), 25=Person(name=Bob, age=25)}
DRuntime exception
Attempts:
2 left
💡 Hint
When keys are duplicated, the last element wins in associateBy.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code cause a compilation error?
Examine this Kotlin code snippet. It tries to create a map using associate but fails to compile. What is the cause?
Kotlin
val numbers = listOf(1, 2, 3)
val map = numbers.associate { it * 2 }
println(map)
AThe list cannot be used with associate on Int elements.
BThe println statement is missing parentheses.
CThe associate function requires a map as input.
DThe lambda must return a Pair, but it returns an Int.
Attempts:
2 left
💡 Hint
Check what the lambda inside associate should return.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using associateWith?
Given this Kotlin code using associateWith, what will be printed?
Kotlin
val words = listOf("one", "two", "three")
val map = words.associateWith { it.length }
println(map)
A{one=3, two=3, three=5}
B{3=one, 3=two, 5=three}
C{one=3, two=3, three=6}
DCompilation error
Attempts:
2 left
💡 Hint
associateWith creates a map where each element is the key and the lambda result is the value.
🧠 Conceptual
expert
2:00remaining
How many entries are in the map created by this Kotlin code?
Consider this Kotlin code that creates a map from a list of pairs using associate. How many entries will the resulting map have?
Kotlin
val pairs = listOf("a" to 1, "b" to 2, "a" to 3, "c" to 4)
val map = pairs.associate { it }
println(map.size)
A3
B4
C2
DCompilation error
Attempts:
2 left
💡 Hint
When keys repeat, the last value overwrites previous ones in a map.