Coroutines in Kotlin provide a powerful way to write asynchronous code in a sequential manner. Understanding their basic concepts, such as suspend functions, launch, and async, is essential for effective Kotlin development.
Topics and MCQs
1. Introduction to Coroutines
What is the main advantage of using coroutines in Kotlin? a) They allow concurrent execution of functions. b) They simplify writing multi-threaded code. c) They improve CPU efficiency. d) They enhance memory management.
What is the fundamental concept behind Kotlin coroutines? a) Running multiple tasks on a single thread b) Handling tasks synchronously c) Using threads for parallel execution d) Allowing asynchronous code to run sequentially
Which Kotlin function is used to launch a new coroutine? a) launch() b) start() c) run() d) coroutineStart()
In which scope can coroutines be launched? a) Global scope b) Local scope c) Coroutine scope d) Both a and c
What is the default dispatcher for coroutines launched in Kotlin? a) Main b) IO c) Default d) Unconfined
Which of the following best describes the nature of coroutines? a) Lightweight threads b) Blocking operations c) Multi-threaded processes d) Independent systems
What happens when a coroutine is suspended? a) It stops running completely. b) It continues execution in the background. c) It releases the thread and resumes later. d) It terminates the program.
Which of the following is not a valid coroutine scope in Kotlin? a) CoroutineScope() b) MainScope() c) GlobalScope() d) TaskScope()
Which function is used to delay the execution of a coroutine? a) wait() b) delay() c) sleep() d) pause()
What does runBlocking do in Kotlin coroutines? a) Runs a coroutine in a blocking manner b) Launches a new thread c) Suspends the current thread d) None of the above
2. Suspend Functions
What is the purpose of the suspend keyword in Kotlin? a) To define a function that can run concurrently b) To mark a function that can be paused and resumed c) To mark a function that requires multithreading d) To make a function synchronous
Which of the following functions can be marked as suspend? a) Only functions that are part of coroutines b) Any function c) Functions that perform long-running tasks d) Functions that are called on a background thread
Which statement is true about a suspend function? a) It can only be called from another suspend function or a coroutine b) It is automatically executed in the background c) It blocks the calling thread d) It executes synchronously
What happens if you call a suspend function outside a coroutine? a) It executes normally. b) It throws an exception. c) It runs on a separate thread. d) It waits indefinitely.
Which of the following is a valid usage of a suspend function? a) Calling it inside an async block b) Calling it directly in the main function c) Using it to create threads manually d) Declaring it inside a non-suspend function
How does a suspend function differ from a regular function? a) It can pause execution without blocking the thread. b) It runs only in the background. c) It consumes more memory. d) It always runs on the main thread.
Which function is used to suspend a coroutine? a) delay() b) wait() c) pause() d) sleep()
What is required to call a suspend function from a coroutine? a) Calling a blocking function before it b) It must be called from another suspend function or a coroutine c) It must run on the main thread d) The function should be wrapped in runBlocking()
Which of the following can not be a suspend function? a) Functions that block the thread b) Functions that delay without blocking c) Functions that call other suspend functions d) Functions with a return type of Unit
What happens when a suspend function is called within another suspend function? a) It causes an error. b) The second function is executed first. c) The second function is executed asynchronously. d) It is executed synchronously in the same thread.
3. Basics of Launch and Async
What is the main difference between launch and async in Kotlin coroutines? a) launch returns a result, while async does not. b) launch runs a coroutine without returning a result, while async returns a Deferred object. c) launch blocks the current thread, while async does not. d) launch can only run on the main thread.
Which coroutine builder is used when you don’t expect a result from a coroutine? a) launch() b) async() c) runBlocking() d) execute()
What does async return when called in a coroutine? a) A result directly b) A future task c) A Deferred object d) A Job object
How can you retrieve the result from an async coroutine in Kotlin? a) By calling await() on the Deferred object b) By calling join() on the Job object c) By using runBlocking() d) By calling defer()
Which of the following functions is typically used to launch a coroutine that performs some non-blocking work? a) runBlocking() b) async() c) launch() d) startCoroutine()
What is the role of Deferred in Kotlin coroutines? a) It represents the result of an asynchronous computation. b) It stores a coroutine’s state. c) It launches a coroutine. d) It schedules coroutines for execution.
Which of the following coroutine builders does not block the calling thread? a) launch() b) runBlocking() c) async() d) Both a and c
In which scenario would you use launch instead of async? a) When you need the result of the coroutine. b) When the coroutine returns a value. c) When you only want to execute a background task. d) When you want to cancel the coroutine immediately.
Which method do you use to wait for a result from an async coroutine? a) await() b) join() c) wait() d) get()
What will happen if you call await() on a Deferred object from a coroutine? a) The coroutine will run synchronously. b) It will block the thread until the result is available. c) It will start a new coroutine. d) It will return an error immediately.
Answers Table
QNo
Answer (Option with Text)
1
b) They simplify writing multi-threaded code
2
d) Allowing asynchronous code to run sequentially
3
a) launch()
4
d) Both a and c
5
c) Default
6
a) Lightweight threads
7
c) It releases the thread and resumes later
8
d) TaskScope()
9
b) delay()
10
a) Runs a coroutine in a blocking manner
11
b) To mark a function that can be paused and resumed
12
c) Functions that perform long-running tasks
13
a) It can only be called from another suspend function or a coroutine
14
b) It throws an exception
15
a) Calling it inside an async block
16
a) It can pause execution without blocking the thread
17
a) delay()
18
b) It must be called from another suspend function or a coroutine
19
a) Functions that block the thread
20
d) It is executed synchronously in the same thread
21
b) launch runs a coroutine without returning a result, while async returns a Deferred object
22
a) launch()
23
c) A Deferred object
24
a) By calling await() on the Deferred object
25
c) launch()
26
a) It represents the result of an asynchronous computation
27
d) Both a and c
28
c) When you only want to execute a background task
29
a) await()
30
b) It will block the thread until the result is available