Bird
0
0

What is the output of this Kotlin code?

medium📝 query result Q13 of 15
Kotlin - Collections Fundamentals
What is the output of this Kotlin code?
val numbers = listOf(1, 2, 3, 4)
println(numbers.size)
println(numbers.isEmpty())
println(numbers.isNotEmpty())
A4 true false
B3 true false
C4 false true
DError: size is not a property
Step-by-Step Solution
Solution:
  1. Step 1: Check the size of the list

    The list has 4 elements, so numbers.size returns 4.
  2. Step 2: Evaluate emptiness checks

    numbers.isEmpty() returns false because the list is not empty.
    numbers.isNotEmpty() returns true because the list has elements.
  3. Final Answer:

    4 false true -> Option C
  4. Quick Check:

    Size = 4, empty? false, not empty? true [OK]
Quick Trick: Remember size counts elements; isEmpty() returns false if not empty [OK]
Common Mistakes:
MISTAKES
  • Thinking size is zero-based index
  • Confusing isEmpty() and isNotEmpty() results
  • Assuming size is a function, causing error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes