Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Collections Fundamentals
What will be the output of this Kotlin code?
val list = listOf("a", "b", "c")
// list.add("d")
println(list.size)

Note: The add() line is commented out.
A4
B3
CCompilation error
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand listOf() and commented code

    listOf() creates an immutable list; add() is commented out, so no change.
  2. Step 2: Check list size

    Original list has 3 elements; size remains 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    Immutable list size unchanged = 3 [OK]
Quick Trick: Immutable list size stays same; add() not allowed [OK]
Common Mistakes:
MISTAKES
  • Assuming add() works on immutable list
  • Expecting size to be 4
  • Confusing commented code effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes