MCQs on Concurrency and Parallelism | Ruby

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

  1. 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
  2. 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
  3. 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
  4. 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
  5. What is the method to safely terminate a Ruby thread?
    a) Thread.exit
    b) Thread.kill
    c) Thread.terminate
    d) Thread.stop
  6. 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
  7. 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
  8. 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
  9. 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?
  10. 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

  1. 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
  2. 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
  3. How do you start a fiber in Ruby?
    a) Fiber.start
    b) Fiber.new
    c) Fiber.run
    d) Fiber.create
  4. What method is used to resume a fiber in Ruby?
    a) Fiber.continue
    b) Fiber.resume
    c) Fiber.run
    d) Fiber.start
  5. Which method is used to pause the execution of a fiber?
    a) Fiber.wait
    b) Fiber.suspend
    c) Fiber.pause
    d) Fiber.block
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. Which of the following is a method provided by concurrent-ruby to handle concurrency?
    a) Future
    b) Thread
    c) Job
    d) Async
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

QNoAnswer
1b) A lightweight process that runs independently
2a) Thread.new
3c) Thread.sleep
4c) Thread.value
5b) Thread.kill
6a) Thread.join
7c) Improve I/O-bound operations
8c) The object representing the currently executing thread
9a) Thread.alive?
10b) By defining parameters in the block passed to Thread.new
11a) A lightweight thread that can be paused and resumed
12a) Fibers run on a single thread, while threads can run concurrently
13b) Fiber.new
14b) Fiber.resume
15b) Fiber.suspend
16b) Fibers use less memory and allow cooperative multitasking
17b) Yes, fibers allow for cooperative multitasking in asynchronous scenarios
18a) Threads are scheduled by the Ruby interpreter, while fibers are scheduled manually
19a) It is stored in memory
20c) Fiber.new
21b) An abstraction for asynchronous programming
22c) It provides abstractions for concurrent and parallel programming
23d) Async do ... end
24b) It allows other tasks to run while waiting for the current task
25a) Future
26b) async is non-blocking and cooperative, while Thread is preemptive and independent
27a) Concurrent::Future.new
28b) Networking and I/O-bound tasks with concurrency
29b) Non-blocking I/O and cooperative multitasking
30c) Async { ... }

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