Bird
0
0

You have a list of user names and want to print "No users" if the list is empty, otherwise print the count. Which Kotlin code correctly does this?

hard📝 Application Q8 of 15
Kotlin - Collections Fundamentals
You have a list of user names and want to print "No users" if the list is empty, otherwise print the count. Which Kotlin code correctly does this?
Aif (users.count() > 0) println("No users") else println(users.size)
Bif (users.isEmpty()) println("No users") else println(users.size)
Cif (users.isNotEmpty()) println("No users") else println(users.size)
Dif (users.size == 0) println(users.size) else println("No users")
Step-by-Step Solution
Solution:
  1. Step 1: Check condition for empty list

    Use isEmpty() to check if list has no elements.
  2. Step 2: Print correct output

    If empty, print "No users"; else print the size.
  3. Final Answer:

    if (users.isEmpty()) println("No users") else println(users.size) -> Option B
  4. Quick Check:

    Use isEmpty() to detect empty list and print accordingly [OK]
Quick Trick: Use isEmpty() to check empty list before printing count [OK]
Common Mistakes:
MISTAKES
  • Reversing condition logic
  • Using isNotEmpty() incorrectly
  • Printing wrong messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes