MCQs on Concurrency | Rust

Rust provides powerful tools for managing concurrency while ensuring safety and performance. This includes threads, shared state concurrency with Mutex, message passing through channels, and leveraging Rust’s ownership model for safe concurrency. These features help prevent data races and ensure reliable concurrent execution.


Concurrency in Rust – MCQs

Part 1: Threads and Spawning Threads

  1. Which function is used to spawn a new thread in Rust?
    • A) spawn_thread()
    • B) std::thread::spawn()
    • C) create_thread()
    • D) start_thread()
  2. What does the join() method do in Rust threads?
    • A) It waits for the thread to finish before continuing.
    • B) It immediately terminates the thread.
    • C) It creates a new thread.
    • D) It starts the thread execution.
  3. Which of the following is correct for passing data to a thread in Rust?
    • A) Data is passed by reference automatically.
    • B) Data must be cloned or moved into the thread.
    • C) Data is passed by pointer.
    • D) Data cannot be passed to threads.
  4. What is the purpose of the move keyword when spawning a thread in Rust?
    • A) It moves the thread to another system.
    • B) It allows ownership of variables to be moved into the thread.
    • C) It prioritizes the thread’s execution.
    • D) It pauses the execution of the thread.
  5. Which of the following is true about Rust threads?
    • A) Threads in Rust are always executed concurrently.
    • B) Threads can run in parallel or concurrently, depending on the system.
    • C) Threads are blocked by default in Rust.
    • D) Threads are never executed concurrently in Rust.
  6. How does Rust ensure memory safety when using threads?
    • A) By disallowing any thread creation.
    • B) By enforcing borrowing rules even across threads.
    • C) By using a garbage collector.
    • D) By manually managing memory allocation.
  7. Which of the following is an example of spawning a thread in Rust?
    • A) thread.spawn { println!("Hello, world!"); }
    • B) std::thread::spawn(|| println!("Hello, world!"));
    • C) thread::start(|| println!("Hello, world!"));
    • D) spawn(|| println!("Hello, world!"));
  8. Can you spawn multiple threads concurrently in Rust?
    • A) Yes, Rust supports spawning multiple threads concurrently.
    • B) No, Rust only allows single-threaded execution.
    • C) Only two threads can be spawned in Rust.
    • D) Only one thread can be spawned at a time in Rust.
  9. What happens if you don’t call join() on a spawned thread in Rust?
    • A) The thread is automatically joined when the main thread exits.
    • B) The thread will be detached, and you can’t retrieve its result.
    • C) The thread will terminate immediately.
    • D) The program will crash.
  10. Which trait is implemented by Rust threads to allow them to be spawned and executed?
    • A) ThreadTrait
    • B) Threadable
    • C) Fn
    • D) Send

Part 2: Shared State Concurrency and the Mutex

  1. What is a Mutex used for in Rust?
  • A) To allow multiple threads to access shared data simultaneously.
  • B) To prevent data from being shared between threads.
  • C) To safely share data between threads by locking it.
  • D) To prevent memory leaks in shared data.
  1. How does a Mutex ensure safe access to shared data in Rust?
  • A) It uses locks to guarantee that only one thread can access data at a time.
  • B) It copies data between threads.
  • C) It allows all threads to access data simultaneously.
  • D) It restricts access to data based on time intervals.
  1. What happens when a thread tries to access data protected by a Mutex in Rust and it is already locked?
  • A) The thread gets blocked until the lock is released.
  • B) The thread is aborted immediately.
  • C) The thread is allowed to access the data with a read-only lock.
  • D) The thread gets a new lock automatically.
  1. In Rust, what is required to unlock a Mutex after acquiring the lock?
  • A) It is unlocked manually with the unlock() method.
  • B) It is unlocked automatically once the thread finishes execution.
  • C) It is unlocked by calling mutex.release().
  • D) The Mutex remains locked until the program ends.
  1. Which of the following is true about Rust’s ownership model and Mutex?
  • A) Mutex cannot be used with types that implement Copy.
  • B) Mutex guarantees data races are impossible.
  • C) Mutex prevents all forms of concurrent access, even with borrowing.
  • D) Mutex allows mutable data to be borrowed multiple times.
  1. What does the lock() method return when acquiring a Mutex in Rust?
  • A) A reference to the data inside the Mutex.
  • B) A Result, which can either be Ok or Err.
  • C) The Mutex itself.
  • D) A cloned copy of the data inside the Mutex.
  1. What does the MutexGuard struct represent in Rust?
  • A) A locked handle to the Mutex that ensures the lock is released when it goes out of scope.
  • B) A pointer to the Mutex data.
  • C) A method for unlocking the Mutex.
  • D) A thread-safe data structure.
  1. Which of the following is an important characteristic of Mutex in Rust?
  • A) Only one thread can hold the lock at any given time.
  • B) Multiple threads can hold the lock concurrently.
  • C) The Mutex is automatically unlocked when it is not in use.
  • D) Mutex cannot be used in Rust.
  1. Can a Mutex be used to protect non-mutable data in Rust?
  • A) Yes, Mutex can protect non-mutable data.
  • B) No, Mutex can only protect mutable data.
  • C) Only RefCell can be used for non-mutable data.
  • D) Mutex is for ensuring thread-safe access to data but only for mutable data.
  1. How can you share a Mutex between multiple threads in Rust?
  • A) By wrapping it in an Arc (atomic reference counter).
  • B) By directly passing it between threads.
  • C) By using a global variable.
  • D) By copying the data inside the Mutex.

Part 3: Channels for Message Passing

  1. What is the purpose of channels in Rust?
  • A) To send data between threads.
  • B) To manage memory allocation.
  • C) To synchronize the execution of threads.
  • D) To directly modify shared memory.
  1. Which Rust type is used for message passing between threads?
  • A) Arc
  • B) Mutex
  • C) Channel
  • D) Sync
  1. How does a Rust channel work to send messages between threads?
  • A) It allows one thread to send data and another thread to receive it through the channel.
  • B) It creates a global memory space for data.
  • C) It directly modifies the memory of another thread.
  • D) It sends data between threads without any synchronization.
  1. Which method is used to send a message through a channel in Rust?
  • A) send()
  • B) pass()
  • C) push()
  • D) transmit()
  1. What does the recv() method do in Rust’s channels?
  • A) It sends a message to the channel.
  • B) It receives a message from the channel.
  • C) It closes the channel.
  • D) It cancels the transmission.
  1. What type of value does the recv() method return when reading from a channel in Rust?
  • A) A boolean indicating success or failure.
  • B) A Result containing the message or an error.
  • C) A reference to the message in the channel.
  • D) A clone of the message.
  1. What does the channel() function do in Rust?
  • A) It creates a pair of transmitting and receiving ends for a channel.
  • B) It initializes the main thread.
  • C) It stops the transmission.
  • D) It locks shared data.
  1. Can multiple threads simultaneously send messages to a channel in Rust?
  • A) Yes, if the channel is of the mpsc (multiple producer, single consumer) type.
  • B) No, only one thread can send messages at a time.
  • C) Yes, if the channel is of the spmc (single producer, multiple consumer) type.
  • D) No, the channel can only be used by one thread.
  1. What happens if a thread tries to receive a message from an empty channel in Rust?
  • A) The program terminates with an error.
  • B) The thread blocks until a message is available.
  • C) The thread retries sending a message.
  • D) The thread returns None.
  1. What is the benefit of using channels for communication between threads in Rust?
  • A) It allows safe message passing without shared mutable state.
  • B) It provides more efficient memory management.
  • C) It allows direct modification of another thread’s memory.
  • D) It automatically synchronizes the threads.

Answer Key

QnoAnswer
1B) std::thread::spawn()
2A) It waits for the thread to finish before continuing.
3B) Data must be cloned or moved into the thread.
4B) It allows ownership of variables to be moved into the thread.
5B) Threads can run in parallel or concurrently, depending on the system.
6B) By enforcing borrowing rules even across threads.
7B) `std::thread::spawn(
8A) Yes, Rust supports spawning multiple threads concurrently.
9B) The thread will be detached, and you can’t retrieve its result.
10D) Send
11C) To safely share data between threads by locking it.
12A) It uses locks to guarantee that only one thread can access data at a time.
13A) The thread gets blocked until the lock is released.
14B) It is unlocked automatically once the thread finishes execution.
15A) Mutex cannot be used with types that implement Copy.
16B) A Result, which can either be Ok or Err.
17A) A locked handle to the Mutex that ensures the lock is released when it goes out of scope.
18A) Only one thread can hold the lock at any given time.
19A) Yes, Mutex can protect non-mutable data.
20A) By wrapping it in an Arc (atomic reference counter).
21A) To send data between threads.
22C) Channel
23A) It allows one thread to send data and another thread to receive it through the channel.
24A) send()
25B) It receives a message from the channel.
26B) A Result containing the message or an error.
27A) It creates a pair of transmitting and receiving ends for a channel.
28A) Yes, if the channel is of the mpsc type.
29B) The thread blocks until a message is available.
30A) It allows safe message passing without shared mutable state.

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