0
0
Kotlinprogramming~20 mins

Collection size and emptiness checks in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collection Size and Emptiness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Check if a list is empty using Kotlin
Given the Kotlin list val fruits = listOf("apple", "banana", "cherry"), what is the output of fruits.isEmpty()?
Kotlin
val fruits = listOf("apple", "banana", "cherry")
println(fruits.isEmpty())
Afalse
Btrue
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Think about whether the list has any items inside.
query_result
intermediate
2:00remaining
Count elements in a Kotlin set
What is the output of the following Kotlin code?
val numbers = setOf(1, 2, 3, 4, 5)
println(numbers.size)
Kotlin
val numbers = setOf(1, 2, 3, 4, 5)
println(numbers.size)
A0
B4
CCompilation error
D5
Attempts:
2 left
💡 Hint
Count how many unique numbers are in the set.
📝 Syntax
advanced
2:00remaining
Identify the correct way to check if a Kotlin map is empty
Which of the following Kotlin expressions correctly checks if the map val map = mapOf("a" to 1, "b" to 2) is empty?
Kotlin
val map = mapOf("a" to 1, "b" to 2)
Amap.isEmpty()
Bmap.empty()
Cmap.size == null
Dmap.isEmpty
Attempts:
2 left
💡 Hint
Look for the standard Kotlin function to check emptiness.
query_result
advanced
2:00remaining
Result of filtering a list and checking size
What is the output of this Kotlin code?
val nums = listOf(1, 2, 3, 4, 5)
val filtered = nums.filter { it > 3 }
println(filtered.size)
Kotlin
val nums = listOf(1, 2, 3, 4, 5)
val filtered = nums.filter { it > 3 }
println(filtered.size)
A3
B2
C5
D0
Attempts:
2 left
💡 Hint
Count how many numbers are greater than 3.
🧠 Conceptual
expert
3:00remaining
Understanding Kotlin collection emptiness and size properties
Consider a Kotlin collection val col = listOf(). Which statement is true about col.isEmpty() and col.size?
A<code>col.isEmpty()</code> returns false and <code>col.size</code> returns 0
B<code>col.isEmpty()</code> returns true and <code>col.size</code> returns null
C<code>col.isEmpty()</code> returns true and <code>col.size</code> returns 0
D<code>col.isEmpty()</code> returns false and <code>col.size</code> returns null
Attempts:
2 left
💡 Hint
Think about what an empty list means for size and emptiness.