0
0
Kotlinprogramming~5 mins

List creation (listOf, mutableListOf) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does listOf() create in Kotlin?
listOf() creates an immutable list, which means you cannot change its elements after creation.
Click to reveal answer
beginner
What is the difference between listOf() and mutableListOf()?
listOf() creates an immutable list, while mutableListOf() creates a mutable list that allows adding, removing, or changing elements.
Click to reveal answer
beginner
How do you add an element to a list created with mutableListOf()?
Use the add() function. For example: val list = mutableListOf(1, 2); list.add(3) adds 3 to the list.
Click to reveal answer
beginner
Can you change elements in a list created with listOf()?
No, lists created with listOf() are immutable. You cannot add, remove, or change elements.
Click to reveal answer
beginner
Write Kotlin code to create a mutable list of strings with elements "apple" and "banana".
val fruits = mutableListOf("apple", "banana")
Click to reveal answer
Which Kotlin function creates an immutable list?
AsetOf()
BlistOf()
CarrayListOf()
DmutableListOf()
How do you add an element to a mutable list in Kotlin?
AUsing add() function
BUsing set() function
CUsing push() function
DUsing insert() function
What happens if you try to add an element to a list created with listOf()?
ARuntime exception
BElement is added successfully
CCompilation error
DElement replaces the last item
Which of these is a mutable list in Kotlin?
Aval list = listOf(1, 2, 3)
Bval list = arrayOf(1, 2, 3)
Cval list = setOf(1, 2, 3)
Dval list = mutableListOf(1, 2, 3)
Which method would you use to remove an element from a mutable list?
Apop()
Bdelete()
Cremove()
Ddiscard()
Explain the difference between listOf() and mutableListOf() in Kotlin.
Think about whether you can change the list after making it.
You got /4 concepts.
    How would you create a list of numbers that you can change later? Write the Kotlin code.
    Use mutableListOf and add some numbers inside parentheses.
    You got /3 concepts.