MCQs on Coroutines | Lua

1. What are Coroutines?

  1. What is a coroutine in Lua?
    • a) A function that runs asynchronously
    • b) A function that can pause and resume execution
    • c) A synchronous function
    • d) A function used to handle errors
  2. How do coroutines differ from regular functions in Lua?
    • a) Coroutines can pause and resume execution
    • b) Coroutines always execute in the background
    • c) Coroutines cannot accept arguments
    • d) Coroutines are faster than regular functions
  3. Which of the following is true about coroutines in Lua?
    • a) They allow multiple tasks to run concurrently
    • b) They can only be executed once
    • c) They operate independently without affecting the main program
    • d) They are used to handle memory management
  4. Why are coroutines useful in Lua programming?
    • a) They allow non-blocking asynchronous code
    • b) They simplify debugging
    • c) They reduce memory usage
    • d) They automatically optimize the program’s performance
  5. What is the key feature of coroutines in Lua?
    • a) Ability to pause and resume execution
    • b) Ability to call other functions
    • c) Ability to run in parallel threads
    • d) Ability to generate random numbers
  6. In Lua, coroutines are most commonly used for:
    • a) Handling input/output
    • b) Performing asynchronous tasks
    • c) Performing mathematical calculations
    • d) Managing global variables
  7. What is one limitation of coroutines in Lua?
    • a) They cannot run in parallel threads
    • b) They only work with local variables
    • c) They cannot interact with external libraries
    • d) They require more memory than regular functions

2. Creating and Managing Coroutines

  1. Which function is used to create a coroutine in Lua?
    • a) coroutine.create()
    • b) coroutine.new()
    • c) coroutine.start()
    • d) coroutine.begin()
  2. What does the coroutine.create() function do in Lua?
    • a) It creates a new coroutine
    • b) It starts the execution of a coroutine
    • c) It stops a coroutine
    • d) It pauses a coroutine
  3. What is the function used to resume a coroutine in Lua?
    • a) coroutine.resume()
    • b) coroutine.start()
    • c) coroutine.execute()
    • d) coroutine.run()
  4. Which of the following is a valid statement about coroutine.resume() in Lua?
    • a) It starts a coroutine’s execution
    • b) It pauses the coroutine
    • c) It checks the status of the coroutine
    • d) It terminates a coroutine
  5. How does the coroutine.yield() function behave?
    • a) It pauses the coroutine and allows other code to run
    • b) It resumes the coroutine from the point it was paused
    • c) It starts a coroutine’s execution
    • d) It creates a new coroutine
  6. What is the primary purpose of coroutine.yield() in Lua?
    • a) To pause the execution of the coroutine
    • b) To create a new coroutine
    • c) To check the status of the coroutine
    • d) To stop the coroutine
  7. How do you check if a coroutine has finished executing in Lua?
    • a) By using coroutine.status()
    • b) By calling coroutine.isfinished()
    • c) By using coroutine.check()
    • d) By checking the return value of coroutine.resume()
  8. What will coroutine.status() return if a coroutine has completed its execution?
    • a) "dead"
    • b) "running"
    • c) "suspended"
    • d) "active"
  9. What does coroutine.status() return if a coroutine is currently running?
    • a) "running"
    • b) "suspended"
    • c) "dead"
    • d) "active"
  10. When is the coroutine.resume() function typically used in Lua?
    • a) After creating a coroutine with coroutine.create()
    • b) After the coroutine has finished execution
    • c) During the creation of a new coroutine
    • d) Before creating a coroutine
  11. Which of the following is true about coroutines in Lua?
    • a) A coroutine starts running immediately after being created
    • b) Coroutines can be started at any time by using coroutine.resume()
    • c) A coroutine cannot be paused once started
    • d) Coroutines execute in parallel threads by default
  12. How do you pass arguments to a coroutine in Lua?
    • a) By passing arguments to coroutine.create()
    • b) By using coroutine.resume()
    • c) By passing arguments inside coroutine.yield()
    • d) By using global variables
  13. What does the return value of coroutine.resume() indicate in Lua?
    • a) If the coroutine was successfully resumed
    • b) The status of the coroutine
    • c) The number of arguments passed to the coroutine
    • d) Whether the coroutine is paused or running

3. Applications of Coroutines

  1. Which of the following is a common application of coroutines in Lua?
    • a) Handling time-consuming or blocking tasks without freezing the program
    • b) Performing parallel computations
    • c) Running system-level tasks asynchronously
    • d) Creating multithreading environments
  2. How can coroutines be useful in game programming?
    • a) To manage animation frames and other non-blocking tasks
    • b) To calculate physics in real-time
    • c) To handle memory management automatically
    • d) To handle networking tasks
  3. What is an example of a real-world scenario where coroutines are useful?
    • a) Handling user input while performing background computations
    • b) Performing high-priority calculations
    • c) Interfacing with external hardware
    • d) Compressing large files
  4. In which of the following tasks can coroutines be applied?
    • a) Handling user interfaces and animations without freezing the game
    • b) Performing tasks that require multiple threads
    • c) Running multiple instances of the same function simultaneously
    • d) Allocating memory efficiently
  5. How are coroutines typically used for cooperative multitasking?
    • a) By manually yielding control between tasks in the program
    • b) By running tasks in parallel threads
    • c) By scheduling tasks automatically
    • d) By dividing the program into independent processes
  6. In Lua, how can coroutines be used to simulate the execution of multiple tasks?
    • a) By suspending and resuming tasks at specific points in the code
    • b) By executing tasks simultaneously in different threads
    • c) By running all tasks sequentially in the main program
    • d) By executing tasks in a loop
  7. Can coroutines in Lua be used for networking applications?
    • a) Yes, to handle multiple connections without blocking the main thread
    • b) No, coroutines are not suitable for networking tasks
    • c) Yes, but only for simple HTTP requests
    • d) No, coroutines are only for CPU-intensive tasks
  8. How do coroutines benefit asynchronous programming in Lua?
    • a) They allow code execution to be paused and resumed without blocking the main thread
    • b) They enable multi-threaded parallel execution
    • c) They make the program run faster
    • d) They prevent the program from executing synchronous functions
  9. What is a key advantage of using coroutines in Lua compared to threads?
    • a) Coroutines use less memory and do not require thread management
    • b) Coroutines run in separate processes
    • c) Coroutines can execute multiple tasks simultaneously
    • d) Coroutines automatically handle resource allocation
  10. How can coroutines help in implementing finite state machines in Lua?
    • a) By maintaining state transitions without blocking execution
    • b) By managing multiple threads for each state
    • c) By automatically handling state changes
    • d) By simplifying the logic of the state machine

Answer Key

QnoAnswer (Option with the text)
1b) A function that can pause and resume execution
2a) Coroutines can pause and resume execution
3a) They allow multiple tasks to run concurrently
4a) They allow non-blocking asynchronous code
5a) Ability to pause and resume execution
6b) Performing asynchronous tasks
7a) They cannot run in parallel threads
8a) coroutine.create()
9a) It creates a new coroutine
10a) coroutine.resume()
11a) It starts a coroutine’s execution
12a) It pauses the coroutine and allows other code to run
13a) To pause the execution of the coroutine
14a) By using coroutine.status()
15a) "dead"
16a) "running"
17a) After creating a coroutine with coroutine.create()
18a) A coroutine starts running immediately after being created
19a) By passing arguments to coroutine.resume()
20a) If the coroutine was successfully resumed
21a) Handling time-consuming or blocking tasks without freezing the program
22a) To manage animation frames and other non-blocking tasks
23a) Handling user input while performing background computations
24a) Handling user interfaces and animations without freezing the game
25a) By manually yielding control between tasks in the program
26a) By suspending and resuming tasks at specific points in the code
27a) Yes, to handle multiple connections without blocking the main thread
28a) They allow code execution to be paused and resumed without blocking the main thread
29a) Coroutines use less memory and do not require thread management
30a) By maintaining state transitions without blocking execution

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