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
Step 1: Check the size of the list
The list has 4 elements, so numbers.size returns 4.
Step 2: Evaluate emptiness checks
numbers.isEmpty() returns false because the list is not empty. numbers.isNotEmpty() returns true because the list has elements.
Final Answer:
4
false
true -> Option C
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
Step 1: Check usage of isEmpty
isEmpty is a function and must be called with parentheses: isEmpty().
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.
Final Answer:
Missing parentheses after isEmpty -> Option A
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
Step 1: Check condition for non-empty list
Using users.size > 0 correctly checks if the list has one or more elements.
Step 2: Verify printing statement
The print statement uses string interpolation to show the number of users online.
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.
Final Answer:
if (users.size > 0) println("Online users: ${users.size}") -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.