MCQs on Coroutines (Basics) | Kotlin

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

  1. 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.
  2. 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
  3. Which Kotlin function is used to launch a new coroutine?
    a) launch()
    b) start()
    c) run()
    d) coroutineStart()
  4. In which scope can coroutines be launched?
    a) Global scope
    b) Local scope
    c) Coroutine scope
    d) Both a and c
  5. What is the default dispatcher for coroutines launched in Kotlin?
    a) Main
    b) IO
    c) Default
    d) Unconfined
  6. Which of the following best describes the nature of coroutines?
    a) Lightweight threads
    b) Blocking operations
    c) Multi-threaded processes
    d) Independent systems
  7. 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.
  8. Which of the following is not a valid coroutine scope in Kotlin?
    a) CoroutineScope()
    b) MainScope()
    c) GlobalScope()
    d) TaskScope()
  9. Which function is used to delay the execution of a coroutine?
    a) wait()
    b) delay()
    c) sleep()
    d) pause()
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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.
  5. 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
  6. 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.
  7. Which function is used to suspend a coroutine?
    a) delay()
    b) wait()
    c) pause()
    d) sleep()
  8. 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()
  9. 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
  10. 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

  1. 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.
  2. Which coroutine builder is used when you don’t expect a result from a coroutine?
    a) launch()
    b) async()
    c) runBlocking()
    d) execute()
  3. 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
  4. 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()
  5. 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()
  6. 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.
  7. Which of the following coroutine builders does not block the calling thread?
    a) launch()
    b) runBlocking()
    c) async()
    d) Both a and c
  8. 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.
  9. Which method do you use to wait for a result from an async coroutine?
    a) await()
    b) join()
    c) wait()
    d) get()
  10. 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

QNoAnswer (Option with Text)
1b) They simplify writing multi-threaded code
2d) Allowing asynchronous code to run sequentially
3a) launch()
4d) Both a and c
5c) Default
6a) Lightweight threads
7c) It releases the thread and resumes later
8d) TaskScope()
9b) delay()
10a) Runs a coroutine in a blocking manner
11b) To mark a function that can be paused and resumed
12c) Functions that perform long-running tasks
13a) It can only be called from another suspend function or a coroutine
14b) It throws an exception
15a) Calling it inside an async block
16a) It can pause execution without blocking the thread
17a) delay()
18b) It must be called from another suspend function or a coroutine
19a) Functions that block the thread
20d) It is executed synchronously in the same thread
21b) launch runs a coroutine without returning a result, while async returns a Deferred object
22a) launch()
23c) A Deferred object
24a) By calling await() on the Deferred object
25c) launch()
26a) It represents the result of an asynchronous computation
27d) Both a and c
28c) When you only want to execute a background task
29a) await()
30b) It will block the thread until the result is available

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top