0
0
Kotlinprogramming~20 mins

List creation (listOf, mutableListOf) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin List 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?
Consider the following Kotlin code snippet. What will be printed?
Kotlin
val list = listOf(1, 2, 3)
println(list.size)
println(list[1])
A3\n2
B2\n1
C3\n1
D2\n3
Attempts:
2 left
💡 Hint
Remember that listOf creates an immutable list and indexing starts at 0.
Predict Output
intermediate
2: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)
ARuntime exception: UnsupportedOperationException
BCompilation error: val cannot be reassigned
CThe element 40 is added successfully
DCompilation error: Unresolved reference 'add'
Attempts:
2 left
💡 Hint
listOf creates an immutable list without add method.
Predict Output
advanced
2: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])
A3\nb
B2\nc
C3\nc
DRuntimeException: IndexOutOfBoundsException
Attempts:
2 left
💡 Hint
mutableListOf creates a list you can change by adding elements.
🧠 Conceptual
advanced
2:00remaining
Which statement about listOf and mutableListOf is true?
Choose the correct statement about Kotlin's listOf and mutableListOf functions.
AlistOf creates a mutable list; mutableListOf creates an immutable list
BlistOf creates an immutable list; mutableListOf creates a mutable list
CBoth listOf and mutableListOf create immutable lists
DBoth listOf and mutableListOf create mutable lists
Attempts:
2 left
💡 Hint
Think about whether you can add or remove elements after creation.
Predict Output
expert
3: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)
A3\n4
B4\n4
C3\n3
DRuntimeException: UnsupportedOperationException
Attempts:
2 left
💡 Hint
toMutableList creates a new mutable copy of the original list.