Bird
Raised Fist0
Kotlinprogramming~10 mins

List creation (listOf, mutableListOf) in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - List creation (listOf, mutableListOf)
Start
↓
Choose list type
↓
listOf
↓
Create immutable list
↓
Return list object
↓
Use list in code
↓
End
You decide which list to create: an immutable list with listOf or a mutable list with mutableListOf. Then Kotlin creates the list and returns it for use.
Execution Sample
Kotlin
val fruits = listOf("Apple", "Banana", "Cherry")
val numbers = mutableListOf(1, 2, 3)
numbers.add(4)
println(fruits)
println(numbers)
Creates an immutable list of fruits and a mutable list of numbers, adds a number to the mutable list, then prints both lists.
Execution Table
StepActionList CreatedList TypeList ContentOutput
1Call listOf("Apple", "Banana", "Cherry")fruitsImmutable["Apple", "Banana", "Cherry"]
2Call mutableListOf(1, 2, 3)numbersMutable[1, 2, 3]
3Add 4 to numbers listnumbersMutable[1, 2, 3, 4]
4Print fruits listfruitsImmutable["Apple", "Banana", "Cherry"][Apple, Banana, Cherry]
5Print numbers listnumbersMutable[1, 2, 3, 4][1, 2, 3, 4]
6End of programProgram ends
💡 Program ends after printing both lists.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
fruitsundefined["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]["Apple", "Banana", "Cherry"]
numbersundefinedundefined[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 2 Insights
Why can't we add an element to the list created with listOf?
The list created with listOf is immutable, meaning it cannot be changed after creation. See execution_table step 1 where fruits list is created immutable, and step 3 where only the mutable list numbers is changed.
What happens when we call mutableListOf?
mutableListOf creates a list that can be changed. In execution_table step 2, numbers list is created mutable, allowing step 3 to add an element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of 'numbers' after step 3?
A[1, 2, 3]
B[1, 2, 3, 4]
C[4]
D[]
💡 Hint
Check the 'List Content' column for 'numbers' at step 3 in execution_table.
At which step is the immutable list 'fruits' created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when listOf is called.
If we tried to add an element to 'fruits', what would happen?
AThe element is added successfully.
BThe list silently ignores the addition.
CA compile-time error occurs.
DThe program crashes at runtime.
💡 Hint
Recall that 'fruits' is immutable as shown in variable_tracker and key_moments.
Concept Snapshot
listOf(...) creates an immutable list that cannot be changed.
mutableListOf(...) creates a mutable list that can be changed.
Use listOf for fixed data, mutableListOf to add or remove items.
Immutable lists prevent accidental changes.
Mutable lists allow adding, removing, or updating elements.
Full Transcript
This example shows how Kotlin creates lists using listOf and mutableListOf. First, listOf creates an immutable list called fruits with three strings. Then mutableListOf creates a mutable list called numbers with three integers. We add a number 4 to the numbers list. Finally, we print both lists. The fruits list stays the same because it is immutable. The numbers list changes because it is mutable. This teaches the difference between fixed and changeable lists in Kotlin.

Practice

(1/5)
1. Which Kotlin function creates a list that cannot be changed after it is created?
easy
A. listOf
B. mutableListOf
C. arrayListOf
D. setOf

Solution

  1. Step 1: Understand listOf behavior

    listOf creates a fixed list that cannot be changed after creation.
  2. Step 2: Compare with mutableListOf

    mutableListOf creates a list that can be changed (add/remove items).
  3. Final Answer:

    listOf -> Option A
  4. Quick Check:

    Fixed list = listOf [OK]
Hint: Use listOf for fixed lists, mutableListOf for changeable lists [OK]
Common Mistakes:
  • Confusing mutableListOf as fixed list
  • Thinking arrayListOf is immutable
  • Using setOf which creates a set, not a list
2. Which of the following is the correct syntax to create a mutable list of integers with values 1, 2, and 3?
easy
A. val list = listOf(1, 2, 3)
B. val list = mutableListOf(1, 2, 3)
C. val list = mutableList(1, 2, 3)
D. val list = list(1, 2, 3)

Solution

  1. Step 1: Identify correct function name

    The correct function to create a mutable list is mutableListOf.
  2. Step 2: Check syntax correctness

    Options C and D use invalid function names. val list = listOf(1, 2, 3) creates an immutable list.
  3. Final Answer:

    val list = mutableListOf(1, 2, 3) -> Option B
  4. Quick Check:

    Mutable list syntax = mutableListOf(...) [OK]
Hint: Remember mutableListOf has 'Of' at the end [OK]
Common Mistakes:
  • Using listOf instead of mutableListOf for mutable lists
  • Misspelling mutableListOf as mutableList
  • Using list() which does not exist
3. What will be the output of this Kotlin code?
val list = listOf("apple", "banana", "cherry")
println(list[1])
medium
A. banana
B. IndexOutOfBoundsException
C. cherry
D. apple

Solution

  1. Step 1: Understand list indexing

    Kotlin lists use zero-based indexing, so index 1 refers to the second item.
  2. Step 2: Identify the item at index 1

    The list is ["apple", "banana", "cherry"], so index 1 is "banana".
  3. Final Answer:

    banana -> Option A
  4. Quick Check:

    Index 1 = second item = banana [OK]
Hint: Remember list indexes start at 0 in Kotlin [OK]
Common Mistakes:
  • Thinking index 1 is the first item
  • Expecting an error for valid index
  • Confusing listOf with mutableListOf output
4. What is wrong with this Kotlin code?
val list = listOf(1, 2, 3)
list.add(4)
medium
A. add() method requires a mutableListOf list
B. Syntax error in listOf declaration
C. listOf creates an immutable list; add() is not allowed
D. No error; code runs fine

Solution

  1. Step 1: Check list type

    listOf creates an immutable (read-only) list.
  2. Step 2: Understand add() method

    The add() method is only available on mutable lists, not on immutable lists.
  3. Final Answer:

    listOf creates an immutable list; add() is not allowed -> Option C
  4. Quick Check:

    Immutable list cannot add items [OK]
Hint: Use mutableListOf to add or remove items [OK]
Common Mistakes:
  • Trying to add to listOf list
  • Ignoring immutability of listOf
  • Assuming add() works on all lists
5. You want to create a list of strings that you can add items to later. Which Kotlin code correctly creates this list and adds "orange" to it?
hard
A. val fruits = mutableListOf("apple", "banana") fruits = fruits + "orange"
B. val fruits = listOf("apple", "banana") fruits.add("orange")
C. val fruits = listOf("apple", "banana") fruits += "orange"
D. val fruits = mutableListOf("apple", "banana") fruits.add("orange")

Solution

  1. Step 1: Choose a mutable list

    To add items later, the list must be mutable, so use mutableListOf.
  2. Step 2: Use add() method correctly

    Calling add("orange") on a mutable list adds the item successfully.
  3. Step 3: Check other options

    val fruits = listOf("apple", "banana") fruits.add("orange") tries to add to immutable list (error). val fruits = listOf("apple", "banana") fruits += "orange" uses += on immutable list (creates new list but does not modify original). val fruits = mutableListOf("apple", "banana") fruits = fruits + "orange" tries to reassign val which is not allowed.
  4. Final Answer:

    val fruits = mutableListOf("apple", "banana") fruits.add("orange") -> Option D
  5. Quick Check:

    Mutable list + add() = correct way [OK]
Hint: Use mutableListOf and add() to change list after creation [OK]
Common Mistakes:
  • Adding to immutable list
  • Trying to reassign val variable
  • Using += on immutable list expecting mutation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard