A suspend function is a special function that can pause its execution without blocking the thread, allowing other work to run. It can only be called from another suspend function or a coroutine.
Click to reveal answer
beginner
How do you declare a suspend function in Kotlin?
You declare a suspend function by adding the suspend keyword before the fun keyword, like suspend fun fetchData().
Click to reveal answer
intermediate
Why can't suspend functions be called directly from regular functions?
Suspend functions can pause execution, so they must be called from a coroutine or another suspend function that can handle this pausing. Regular functions don't support this behavior.
Click to reveal answer
beginner
What happens when a suspend function calls delay()?
When a suspend function calls delay(), it pauses the function without blocking the thread, allowing other coroutines to run until the delay finishes.
Click to reveal answer
beginner
Can suspend functions return values? How?
Yes, suspend functions can return values just like regular functions. You use the return keyword to return a value, and the caller receives it when the function resumes.
Click to reveal answer
What keyword is used to declare a suspend function in Kotlin?
Asuspend
Basync
Cawait
Dcoroutine
✗ Incorrect
The suspend keyword marks a function as suspendable.
Which of the following can call a suspend function directly?
ARegular function
BJava main method
CCoroutine or another suspend function
DAny function without restrictions
✗ Incorrect
Suspend functions must be called from a coroutine or another suspend function.
What does a suspend function do when it calls delay()?
APauses without blocking the thread
BRuns in a new thread
CThrows an exception
DBlocks the thread
✗ Incorrect
delay() pauses the suspend function without blocking the thread.
Can suspend functions return values?
AOnly if they run on the main thread
BNo, they cannot return values
COnly if they are marked with <code>async</code>
DYes, just like regular functions
✗ Incorrect
Suspend functions can return values normally using return.
Why are suspend functions useful?
AThey make code run faster by using multiple threads
BThey allow pausing without blocking threads, improving efficiency
CThey replace all regular functions
DThey automatically handle exceptions
✗ Incorrect
Suspend functions let you pause work without blocking threads, so other tasks can run smoothly.
Explain what a suspend function is and why it is important in Kotlin coroutines.
Think about how suspend functions help manage waiting times without freezing the app.
You got /3 concepts.
Describe how you would call a suspend function from a regular Kotlin program.
Remember, you cannot call suspend functions directly from normal functions.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a suspend function in Kotlin?
easy
A. To mark the function as deprecated
B. To make the function run faster by using multiple threads
C. To allow the function to pause and resume without blocking the thread
D. To convert the function into a lambda expression
Solution
Step 1: Understand what suspend functions do
Suspend functions let the program pause execution at some point and resume later without blocking the thread.
Step 2: Compare options with this behavior
Only To allow the function to pause and resume without blocking the thread describes this pausing and resuming behavior correctly.
Final Answer:
To allow the function to pause and resume without blocking the thread -> Option C
Quick Check:
Suspend function purpose = pause and resume without blocking [OK]
Hint: Suspend means pause and resume without blocking [OK]
Common Mistakes:
Thinking suspend means multithreading
Confusing suspend with deprecated functions
Assuming suspend converts functions to lambdas
2. Which of the following is the correct way to declare a suspend function in Kotlin?
easy
A. fun suspend fetchData() { }
B. fun fetchData() suspend { }
C. suspend fetchData fun() { }
D. suspend fun fetchData() { }
Solution
Step 1: Recall suspend function syntax
The correct syntax places the suspend keyword before fun.
Step 2: Check each option
Only suspend fun fetchData() { } matches the correct syntax: suspend fun fetchData() { }.
Final Answer:
suspend fun fetchData() { } -> Option D
Quick Check:
Suspend function syntax = suspend before fun [OK]
Hint: Remember: suspend keyword comes before fun [OK]
Common Mistakes:
Placing suspend after fun
Putting suspend after function name
Mixing order of keywords
3. What will be the output of the following Kotlin code?
import kotlinx.coroutines.*
suspend fun greet() {
delay(100)
println("Hello from suspend")
}
fun main() = runBlocking {
greet()
println("Done")
}
medium
A. Hello from suspend\nDone
B. Done\nHello from suspend
C. Compilation error because greet() is suspend
D. Runtime error because delay() is not allowed here
Solution
Step 1: Understand runBlocking and suspend call
runBlocking starts a coroutine and allows calling suspend functions like greet().
Step 2: Trace execution order
greet() delays 100ms, then prints "Hello from suspend". After greet() finishes, main prints "Done".
Final Answer:
Hello from suspend\nDone -> Option A
Quick Check:
Output order = greet() then Done [OK]
Hint: runBlocking lets you call suspend functions directly [OK]
Common Mistakes:
Assuming greet() runs asynchronously and prints after Done
Thinking delay causes errors outside coroutine
Confusing suspend function calls outside coroutines
4. Identify the error in this Kotlin code snippet:
fun fetchData() {
delay(1000)
println("Data fetched")
}
medium
A. delay() cannot be called from a non-suspend function
B. println() is not allowed inside functions
C. fetchData() must return a value
D. delay() requires an integer argument, 1000 is invalid
Solution
Step 1: Check usage of delay()
delay() is a suspend function and can only be called from suspend functions or coroutines.
Step 2: Analyze fetchData() declaration
fetchData() is a regular function without suspend keyword, so calling delay() inside causes an error.
Final Answer:
delay() cannot be called from a non-suspend function -> Option A
Quick Check:
delay() call requires suspend function context [OK]
Hint: delay() only works inside suspend functions [OK]
Common Mistakes:
Thinking delay() works in normal functions
Believing println() is restricted
Confusing delay() argument type
5. You want to write a suspend function fetchUserData() that calls another suspend function fetchFromNetwork() and returns its result. Which of the following is the correct way to implement fetchUserData()?
hard
A. fun fetchUserData() = fetchFromNetwork()
B. suspend fun fetchUserData() = fetchFromNetwork()
C. fun suspend fetchUserData() = fetchFromNetwork()
D. suspend fun fetchUserData() { fetchFromNetwork() }
Solution
Step 1: Recognize suspend function chaining rules
A suspend function calling another suspend function must itself be marked suspend.
Step 2: Check each option's syntax and behavior
suspend fun fetchUserData() = fetchFromNetwork() correctly declares fetchUserData() as suspend and returns fetchFromNetwork() result. The option with suspend fun fetchUserData() { fetchFromNetwork() } misses returning the result. The remaining options have incorrect syntax.
Final Answer:
suspend fun fetchUserData() = fetchFromNetwork() -> Option B