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?
✗ Incorrect
listOf() creates an immutable list that cannot be changed after creation.How do you add an element to a mutable list in Kotlin?
✗ Incorrect
The
add() function adds an element to a mutable list.What happens if you try to add an element to a list created with
listOf()?✗ Incorrect
Lists created with
listOf() are immutable, so adding elements causes a runtime exception.Which of these is a mutable list in Kotlin?
✗ Incorrect
mutableListOf() creates a mutable list that can be changed.Which method would you use to remove an element from a mutable list?
✗ Incorrect
The
remove() method removes an element from a mutable list.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.