Challenge - 5 Problems
Kotlin List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code?
Consider the following Kotlin code snippet. What will be printed?
Kotlin
val list = listOf(1, 2, 3) println(list.size) println(list[1])
Attempts:
2 left
💡 Hint
Remember that listOf creates an immutable list and indexing starts at 0.
✗ Incorrect
The list has 3 elements: 1, 2, and 3. The size is 3. The element at index 1 is the second element, which is 2.
❓ Predict Output
intermediate2:00remaining
What happens when you try to add an element to a listOf list?
What will happen when running this Kotlin code?
Kotlin
val list = listOf(10, 20, 30) list.add(40)
Attempts:
2 left
💡 Hint
listOf creates an immutable list without add method.
✗ Incorrect
listOf returns an immutable list which does not have an add method. Trying to call add causes a compilation error because the method does not exist.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code using mutableListOf?
Analyze the following Kotlin code and select the output:
Kotlin
val mutableList = mutableListOf("a", "b") mutableList.add("c") println(mutableList.size) println(mutableList[2])
Attempts:
2 left
💡 Hint
mutableListOf creates a list you can change by adding elements.
✗ Incorrect
The mutable list starts with 2 elements. After adding "c", size becomes 3. The element at index 2 is "c".
🧠 Conceptual
advanced2:00remaining
Which statement about listOf and mutableListOf is true?
Choose the correct statement about Kotlin's listOf and mutableListOf functions.
Attempts:
2 left
💡 Hint
Think about whether you can add or remove elements after creation.
✗ Incorrect
listOf creates a list that cannot be changed after creation (immutable). mutableListOf creates a list that can be changed (mutable).
❓ Predict Output
expert3:00remaining
What is the output of this Kotlin code involving list creation and modification?
Examine this Kotlin code and select the output:
Kotlin
val list1 = listOf(1, 2, 3) val list2 = list1.toMutableList() list2.add(4) println(list1.size) println(list2.size)
Attempts:
2 left
💡 Hint
toMutableList creates a new mutable copy of the original list.
✗ Incorrect
list1 is immutable with size 3. list2 is a mutable copy, so after adding 4, its size is 4. list1 remains size 3.