Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Collections Fundamentals
What will be the output of this Kotlin code?
val list = listOf(1, 2, 3)
println(list.size)
ACompilation error
B0
C3
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand listOf creates an immutable list with 3 elements

    The list contains elements 1, 2, and 3, so size is 3.
  2. Step 2: Print the size property

    Printing list.size outputs the number of elements, which is 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    listOf size = 3 [OK]
Quick Trick: listOf size equals number of elements [OK]
Common Mistakes:
MISTAKES
  • Expecting error when accessing size
  • Confusing size with last element
  • Thinking list is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes