Concurrency and Parallelism in Ruby: MCQs for Enhanced Learning
In this chapter, we will explore Ruby’s concurrency and parallelism mechanisms, including threads, fibers, coroutines, and async programming using async and concurrent-ruby libraries.
Topic 1: Threads in Ruby
What is a thread in Ruby? a) A method that executes asynchronously b) A lightweight process that runs independently c) A data structure for storing values d) A synchronous block of code
Which of the following Ruby method is used to create a new thread? a) Thread.new b) Thread.create c) Thread.fork d) Thread.start
What is the method used to pause a thread’s execution in Ruby? a) Thread.stop b) Thread.pause c) Thread.sleep d) Thread.halt
How can you access the result from a Ruby thread? a) By using the Thread.result method b) By calling Thread.join c) By using Thread.value d) Threads do not return results
What is the method to safely terminate a Ruby thread? a) Thread.exit b) Thread.kill c) Thread.terminate d) Thread.stop
Which Ruby method allows the main program to wait for all threads to finish? a) Thread.join b) Thread.sync c) Thread.wait d) Thread.wait_all
What is the key benefit of using threads in Ruby? a) Speed up CPU-bound operations b) Improve single-threaded performance c) Improve I/O-bound operations d) Minimize memory usage
What does Thread.current return in Ruby? a) The name of the current thread b) The parent thread c) The object representing the currently executing thread d) The ID of the current thread
Which of the following methods can be used to check if a Ruby thread is alive? a) Thread.alive? b) Thread.active? c) Thread.running? d) Thread.exists?
How do you pass arguments to a thread in Ruby? a) Using Thread.arguments b) By defining parameters in the block passed to Thread.new c) Passing arguments directly to the thread’s run method d) Ruby threads do not support arguments
Topic 2: Fibers and Coroutines
What is a fiber in Ruby? a) A lightweight thread that can be paused and resumed b) A separate process for executing code c) A data structure used to store thread information d) A block of code executed synchronously
How does a fiber differ from a thread? a) Fibers run on a single thread, while threads can run concurrently b) Fibers cannot be paused c) Fibers consume more resources than threads d) Fibers run concurrently across multiple cores
How do you start a fiber in Ruby? a) Fiber.start b) Fiber.new c) Fiber.run d) Fiber.create
What method is used to resume a fiber in Ruby? a) Fiber.continue b) Fiber.resume c) Fiber.run d) Fiber.start
Which method is used to pause the execution of a fiber? a) Fiber.wait b) Fiber.suspend c) Fiber.pause d) Fiber.block
What is the main advantage of using fibers over threads in Ruby? a) Fibers provide true parallelism b) Fibers use less memory and allow cooperative multitasking c) Fibers are better suited for CPU-bound tasks d) Fibers are automatically executed by the Ruby interpreter
Can fibers be used for asynchronous programming in Ruby? a) No, fibers are for parallel execution only b) Yes, fibers allow for cooperative multitasking in asynchronous scenarios c) Yes, but only in CPU-bound tasks d) No, fibers do not support asynchronous execution
How do fibers and threads differ in terms of scheduling? a) Threads are scheduled by the Ruby interpreter, while fibers are scheduled manually b) Fibers are scheduled automatically by the operating system c) Threads are manually scheduled, while fibers are executed concurrently d) There is no difference between fibers and threads
How is a fiber’s state saved when it is paused? a) It is stored in memory b) The Ruby interpreter keeps track of it automatically c) The fiber’s state is saved in a file d) It is discarded until resumed
Which of the following methods is used to create a new fiber in Ruby? a) Fiber.create b) Fiber.start c) Fiber.new d) Fiber.run
Topic 3: Introduction to Async Programming with async and concurrent-ruby
What does the async gem provide for Ruby? a) A way to define and handle threads b) An abstraction for asynchronous programming c) A tool for handling I/O-bound tasks d) A way to define fibers
Which of the following is the primary feature of the concurrent-ruby gem? a) It enables multi-core parallelism b) It provides async/await functionality c) It provides abstractions for concurrent and parallel programming d) It replaces the need for threads in Ruby
Which method is used to start an asynchronous task in Ruby using async? a) async.new b) async.start c) Async.task d) Async do ... end
What does the await keyword do in the async gem? a) It blocks the current thread until the task is completed b) It allows other tasks to run while waiting for the current task c) It terminates the task after completion d) It starts a new thread for the task
Which of the following is a method provided by concurrent-ruby to handle concurrency? a) Future b) Thread c) Job d) Async
What is the main difference between async and Thread in Ruby? a) async provides synchronous behavior, while Thread is asynchronous b) async is non-blocking and cooperative, while Thread is preemptive and independent c) Thread is for parallel execution, while async is for sequential tasks d) async cannot be used for I/O-bound tasks
Which of the following methods from the concurrent-ruby gem returns a future object? a) Concurrent::Future.new b) Concurrent::Async.new c) Concurrent::Task.new d) Concurrent::Job.new
What does the concurrent-ruby gem help manage in Ruby programs? a) CPU-bound tasks b) Networking and I/O-bound tasks with concurrency c) The memory usage of Ruby programs d) The Ruby interpreter’s performance
Which of the following features is provided by the async gem in Ruby? a) Parallel thread execution b) Non-blocking I/O and cooperative multitasking c) Direct control over system processes d) Low-level thread synchronization
How do you run an asynchronous block of code using the async gem? a) async.run b) async.execute c) Async { ... } d) Async.run
Answers Table
QNo
Answer
1
b) A lightweight process that runs independently
2
a) Thread.new
3
c) Thread.sleep
4
c) Thread.value
5
b) Thread.kill
6
a) Thread.join
7
c) Improve I/O-bound operations
8
c) The object representing the currently executing thread
9
a) Thread.alive?
10
b) By defining parameters in the block passed to Thread.new
11
a) A lightweight thread that can be paused and resumed
12
a) Fibers run on a single thread, while threads can run concurrently
13
b) Fiber.new
14
b) Fiber.resume
15
b) Fiber.suspend
16
b) Fibers use less memory and allow cooperative multitasking
17
b) Yes, fibers allow for cooperative multitasking in asynchronous scenarios
18
a) Threads are scheduled by the Ruby interpreter, while fibers are scheduled manually
19
a) It is stored in memory
20
c) Fiber.new
21
b) An abstraction for asynchronous programming
22
c) It provides abstractions for concurrent and parallel programming
23
d) Async do ... end
24
b) It allows other tasks to run while waiting for the current task
25
a) Future
26
b) async is non-blocking and cooperative, while Thread is preemptive and independent
27
a) Concurrent::Future.new
28
b) Networking and I/O-bound tasks with concurrency