0
0
Kotlinprogramming~5 mins

Collection size and emptiness checks in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What Kotlin function checks if a collection has no elements?
The isEmpty() function returns true if the collection contains no elements.
Click to reveal answer
beginner
How do you check if a Kotlin collection has at least one element?
Use the isNotEmpty() function, which returns true if the collection contains one or more elements.
Click to reveal answer
beginner
What property gives the number of elements in a Kotlin collection?
The size property returns the count of elements in the collection.
Click to reveal answer
intermediate
Why is it better to use isEmpty() instead of size == 0 in Kotlin?
Using isEmpty() is clearer and can be more efficient because it may avoid counting all elements if the collection supports fast emptiness checks.
Click to reveal answer
beginner
What will listOf(1, 2, 3).isNotEmpty() return?
It returns true because the list contains three elements.
Click to reveal answer
Which Kotlin function checks if a collection is empty?
AisEmpty()
BisNotEmpty()
ChasElements()
Dcount()
What does collection.size return in Kotlin?
AThe last element of the collection
BThe number of elements in the collection
CThe first element of the collection
DTrue if collection is empty
Which function returns true if a Kotlin collection has one or more elements?
AisEmpty()
BhasElements()
CisNotEmpty()
Dcount()
Why might isEmpty() be preferred over size == 0?
AIt can be more efficient and clearer
BIt is less readable
CIt always returns false
DIt counts elements twice
What is the result of emptyList<Int>().isNotEmpty()?
AThrows an error
Btrue
Cnull
Dfalse
Explain how to check if a Kotlin collection is empty or not.
Think about functions that return true or false based on collection content.
You got /4 concepts.
    Describe why using isEmpty() might be better than checking size == 0 in Kotlin collections.
    Consider both code clarity and how the collection might be implemented.
    You got /3 concepts.