Challenge - 5 Problems
Collections Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Kotlin code using a List?
Consider this Kotlin code snippet that creates and prints a list. What will be printed?
Android Kotlin
val fruits = listOf("Apple", "Banana", "Cherry") println(fruits[1])
Attempts:
2 left
💡 Hint
Remember that list indices start at zero.
✗ Incorrect
In Kotlin, list indices start at 0, so fruits[1] accesses the second item, which is "Banana".
📝 Syntax
intermediate2:00remaining
What error does this Kotlin Map code produce?
Examine this Kotlin code that tries to create a map. What error will it cause?
Android Kotlin
val map = mapOf("one" to 1, "two" 2, "three" to 3)
Attempts:
2 left
💡 Hint
Check the syntax for key-value pairs in mapOf.
✗ Incorrect
In Kotlin, mapOf requires key-value pairs separated by 'to'. The pair "two" 2 is missing 'to', causing a syntax error.
❓ lifecycle
advanced2:00remaining
What happens when you add duplicate elements to a Kotlin Set?
Given this Kotlin code, what will be the size of the set after adding duplicates?
Android Kotlin
val numbers = mutableSetOf(1, 2, 3) numbers.add(2) numbers.add(4) println(numbers.size)
Attempts:
2 left
💡 Hint
Sets do not allow duplicate elements.
✗ Incorrect
Sets only keep unique elements. Adding 2 again does not increase size. Adding 4 adds a new element. So size is 4.
🔧 Debug
advanced2:00remaining
Why does this Kotlin code throw an exception?
This Kotlin code tries to access a map value. Why does it throw an exception?
Android Kotlin
val map = mapOf("a" to 1, "b" to 2) val value = map["c"]!! println(value)
Attempts:
2 left
💡 Hint
What does the !! operator do when the value is null?
✗ Incorrect
The !! operator asserts non-null. Since map["c"] returns null, !! throws NullPointerException.
🧠 Conceptual
expert3:00remaining
How many items are in this Kotlin collection after these operations?
What is the size of the collection after this Kotlin code runs?
Android Kotlin
val list = listOf(1, 2, 3, 4) val set = list.toMutableSet() set.remove(2) set.add(5) val map = set.associateWith { it * 2 } println(map.size)
Attempts:
2 left
💡 Hint
Track each step: list to set, remove, add, then map size.
✗ Incorrect
Starting with list of 4 items, converted to set (4 unique items). Remove one (2), add one (5), set size remains 4. Map keys come from set, so map size is 4.