Bird
Raised Fist0
Kotlinprogramming~10 mins

Collection size and emptiness checks in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Collection size and emptiness checks
Start with a collection
↓
Check if collection is empty?
↓
Return true
↓
Return true/false
This flow shows how Kotlin checks if a collection is empty or has elements by using isEmpty(), isNotEmpty(), or size properties.
Execution Sample
Kotlin
val list = listOf(1, 2, 3)
println(list.isEmpty())
println(list.isNotEmpty())
println(list.size > 0)
This code checks if the list is empty or not and prints the results.
Execution Table
StepExpressionEvaluationResult
1list.isEmpty()Checks if list has no elementsfalse
2list.isNotEmpty()Checks if list has at least one elementtrue
3list.size > 0Checks if size is greater than zerotrue
4EndAll checks doneExecution stops
💡 All checks completed, results show list is not empty.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
list[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
list.isEmpty()N/Afalsefalsefalsefalse
list.isNotEmpty()N/AN/Atruetruetrue
list.sizeN/AN/AN/A33
list.size > 0N/AN/AN/Atruetrue
Key Moments - 2 Insights
Why does list.isEmpty() return false but list.isNotEmpty() return true?
Because list has elements, isEmpty() returns false (not empty), and isNotEmpty() returns true (has elements). See execution_table rows 1 and 2.
Is checking list.size > 0 the same as list.isNotEmpty()?
Yes, both check if the collection has elements. list.size > 0 returns true if there is at least one element, same as isNotEmpty(). See execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of list.isEmpty() at step 1?
Afalse
Bnull
Ctrue
Derror
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the code confirm the list has elements?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look for where isNotEmpty() returns true in execution_table.
If the list was empty, what would list.size > 0 return?
Atrue
Bfalse
Cnull
Derror
💡 Hint
Think about size of empty list and check execution_table logic.
Concept Snapshot
Kotlin collections have methods to check emptiness:
- isEmpty() returns true if no elements
- isNotEmpty() returns true if at least one element
- size gives number of elements
Use these to quickly check collection state.
Full Transcript
This visual execution shows how Kotlin checks collection emptiness. Starting with a list of three elements, isEmpty() returns false because the list has items. isNotEmpty() returns true confirming presence of elements. Checking size > 0 also returns true. Variables remain unchanged during checks. Key moments clarify why isEmpty and isNotEmpty return opposite booleans and confirm size > 0 is equivalent to isNotEmpty. The quiz tests understanding of these results referencing the execution table. This helps beginners see step-by-step how Kotlin evaluates collection size and emptiness.

Practice

(1/5)
1. Which Kotlin function checks if a collection has no elements?
easy
A. count()
B. isNotEmpty()
C. size()
D. isEmpty()

Solution

  1. Step 1: Understand the purpose of isEmpty()

    This function returns true if the collection has zero elements.
  2. Step 2: Compare with other functions

    isNotEmpty() checks for one or more elements, count() and size return the number of elements but do not return a boolean.
  3. Final Answer:

    isEmpty() -> Option D
  4. Quick Check:

    Check empty collection with isEmpty() = true [OK]
Hint: Use isEmpty() to check for no elements [OK]
Common Mistakes:
  • Confusing isEmpty() with isNotEmpty()
  • Using count() to check emptiness
  • Calling size() as a function instead of property
2. Which of the following is the correct syntax to check if a list named items is not empty in Kotlin?
easy
A. if (items.isNotEmpty()) { ... }
B. if (items.count > 0) { ... }
C. if (items.size() > 0) { ... }
D. if (items.isEmpty) { ... }

Solution

  1. Step 1: Identify correct function call syntax

    In Kotlin, isNotEmpty() is a function and requires parentheses.
  2. Step 2: Check other options for syntax errors

    isEmpty without parentheses is invalid, size is a property, not a function, so size() is wrong, and count is a function, so count > 0 is invalid.
  3. Final Answer:

    if (items.isNotEmpty()) { ... } -> Option A
  4. Quick Check:

    Use isNotEmpty() with parentheses [OK]
Hint: Remember isNotEmpty() needs parentheses [OK]
Common Mistakes:
  • Using property syntax without parentheses for functions
  • Calling size as a function
  • Using count without parentheses
3. What is the output of this Kotlin code?
val numbers = listOf(1, 2, 3, 4)
println(numbers.size)
println(numbers.isEmpty())
println(numbers.isNotEmpty())
medium
A. 4 true false
B. 3 true false
C. 4 false true
D. Error: size is not a property

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]
Hint: Remember size counts elements; isEmpty() returns false if not empty [OK]
Common Mistakes:
  • Thinking size is zero-based index
  • Confusing isEmpty() and isNotEmpty() results
  • Assuming size is a function, causing error
4. Identify the error in this Kotlin code snippet:
val data = listOf<String>()
if (data.isEmpty) {
    println("No data")
}
medium
A. Missing parentheses after isEmpty
B. listOf<String>() cannot be empty
C. println syntax is incorrect
D. data should be mutable

Solution

  1. Step 1: Check usage of isEmpty

    isEmpty is a function and must be called with parentheses: isEmpty().
  2. Step 2: Verify other parts of the code

    listOf<String>() creates an empty list correctly, println syntax is correct, and mutability is not required for checking emptiness.
  3. Final Answer:

    Missing parentheses after isEmpty -> Option A
  4. Quick Check:

    Functions need parentheses even if no arguments [OK]
Hint: Always add () when calling functions like isEmpty() [OK]
Common Mistakes:
  • Using property syntax for functions
  • Thinking empty list creation is invalid
  • Assuming mutability is needed for checks
5. You have a list of user names and want to print how many users are online only if the list is not empty. Which Kotlin code snippet correctly does this?
hard
A. if (users.isEmpty()) println("Online users: ${users.size}")
B. if (users.size > 0) println("Online users: ${users.size}")
C. if (users.count == 0) println("Online users: ${users.count}")
D. if (users.isNotEmpty) println("Online users: ${users.size}")

Solution

  1. Step 1: Check condition for non-empty list

    Using users.size > 0 correctly checks if the list has one or more elements.
  2. Step 2: Verify printing statement

    The print statement uses string interpolation to show the number of users online.
  3. Step 3: Analyze incorrect options

    isEmpty() checks for empty, so if (users.isEmpty()) println("Online users: ${users.size}") prints when empty, which is wrong.
    count is a function, so count == 0 is invalid.
    isNotEmpty without parentheses is incorrect syntax.
  4. Final Answer:

    if (users.size > 0) println("Online users: ${users.size}") -> Option B
  5. Quick Check:

    Check size > 0 for non-empty list [OK]
Hint: Use size > 0 or isNotEmpty() with parentheses [OK]
Common Mistakes:
  • Using isEmpty() to check for non-empty
  • Calling count without parentheses
  • Omitting parentheses on isNotEmpty()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard