Concurrency Safety Questions with Answers

15 practice questions on Concurrency Safety (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

What does the Send marker trait mean?

  1. Ownership of the type can be transferred to another thread
  2. The type implements Clone
  3. The type can be printed with {} formatting
  4. The type can be sent over the network

Answer: A. Ownership of the type can be transferred to another thread

Explanation

Most types are Send; Rc<T> is not, because its count is not atomic.

Step-by-step solution

  1. Send marks types whose ownership can move to another thread.
  2. Most types are Send; Rc is not, because its count is not atomic.
  3. So option A is the correct answer.

Why the other options are wrong

  • B. Clone is a separate trait.
  • C. Formatting uses Display or Debug, not Send.
  • D. Send is about threads, not the network.

Tip: If the compiler rejects a move into a thread, check whether the type is Send.

Question 2Easy

What does handle.join() do on a thread JoinHandle?

  1. Starts the thread running its closure
  2. Blocks until the thread finishes and returns its result
  3. Cancels the thread before it finishes
  4. Detaches the thread so it runs in the background

Answer: B. Blocks until the thread finishes and returns its result

Explanation

join waits for the thread to end and gives back a Result with its return value or panic.

Step-by-step solution

  1. Main thread calls join.
  2. It waits.
  3. Returns the thread's result.

Why the other options are wrong

  • A. Spawning starts it.
  • C. Rust has no forced cancel here.
  • D. Dropping the handle detaches.

Question 3Easy

How does Rust treat a data race at compile time?

  1. It is allowed but written to a log at run time
  2. It is always caught only at run time by a checker
  3. Only a warning appears and the build succeeds
  4. Safe code that could cause a data race is rejected

Answer: D. Safe code that could cause a data race is rejected

Explanation

Ownership plus Send and Sync make data races in safe code a compile error.

Step-by-step solution

  1. Shared mutable access is checked.
  2. Unsafe patterns fail to compile.
  3. Hence fearless concurrency.

Why the other options are wrong

  • A. Rust refuses it.
  • B. Run time checks are not the main mechanism.
  • C. This is an error, not a warning.

Question 4Medium

Why is the move keyword often needed in thread::spawn(move || { ... })?

  1. The thread may outlive the current scope, so it must own what it uses
  2. It copies the closure to every CPU core for parallel execution
  3. It is required for every closure
  4. move makes the thread run faster

Answer: A. The thread may outlive the current scope, so it must own what it uses

Explanation

Without move the closure would borrow locals that might be dropped before the thread finishes.

Step-by-step solution

  1. A spawned thread may run after the current function has returned.
  2. Without move, the closure would borrow locals that might be dropped first.
  3. So the closure must own what it uses; option A is the correct answer.

Why the other options are wrong

  • B. Move does not copy the closure to every core.
  • C. Move is not required for every closure.
  • D. Move does not make the thread faster.

Tip: Use move when a closure passed to thread spawn uses values from outside it.

Question 5Medium

What does this program print?

use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
    let c = Arc::new(Mutex::new(0));
    let mut hs = vec![];
    for _ in 0..10 {
        let c = Arc::clone(&c);
        hs.push(thread::spawn(move || { *c.lock().unwrap() += 1; }));
    }
    for h in hs { h.join().unwrap(); }
    println!("{}", *c.lock().unwrap());
}
  1. 0
  2. 1
  3. Any value from 1 to 10
  4. 10

Answer: D. 10

Explanation

Ten threads each take the lock and add 1, and all are joined before the read, so the result is always 10.

Step-by-step solution

  1. The Mutex serialises updates.
  2. join waits for every thread.
  3. Total is 10.

Why the other options are wrong

  • A. All threads are joined before printing.
  • B. Each of the ten threads adds one.
  • C. The mutex serialises the increments, so none are lost.

Question 6Medium

In std::sync::mpsc, what does rx.recv() do when the channel is empty and a sender still exists?

  1. Blocks until a message arrives
  2. Returns None immediately without waiting
  3. Panics straight away with a message
  4. Returns 0 as a default value

Answer: A. Blocks until a message arrives

Explanation

recv blocks the thread until a value is sent, or returns Err once all senders are dropped. try_recv is the non-blocking variant.

Step-by-step solution

  1. Channel empty.
  2. Sender alive.
  3. recv waits.

Why the other options are wrong

  • B. try_recv returns without blocking.
  • C. It does not panic.
  • D. There is no default value.

Question 7Medium

Arc<Mutex<T>> is the standard pattern for:

  1. Sending values through a channel without copying
  2. Shared, mutable state across threads
  3. Single-threaded caching
  4. Read-only constants

Answer: B. Shared, mutable state across threads

Explanation

Arc shares ownership across threads; Mutex ensures exclusive access to the data.

Step-by-step solution

  1. Arc shares ownership of a value across threads.
  2. Mutex lets one thread at a time change the data.
  3. Together they give shared mutable state across threads, so option B is the correct answer.

Why the other options are wrong

  • A. A channel sends values between threads; Arc with Mutex is about shared state.
  • C. Single-threaded caching uses Rc, not Arc with Mutex.
  • D. Read-only constants do not need a Mutex.

Tip: Use Arc with Mutex for shared mutable state, or channels when threads can pass messages.

Question 8Medium

Rust's standard channels (std::sync::mpsc) are:

  1. Single-producer, multiple-consumer
  2. Multiple-producer, multiple-consumer with no restrictions
  3. Multiple-producer, single-consumer
  4. Network sockets

Answer: C. Multiple-producer, single-consumer

Explanation

Clone the Sender for each producer thread; one Receiver consumes.

Step-by-step solution

  1. Std::sync::mpsc channels let many Sender handles send values.
  2. A single Receiver takes all the values, so there is one consumer.
  3. So option C, multiple producers and a single consumer, is the correct answer.

Why the other options are wrong

  • A. This is reversed: the channel has one receiver, not many producers.
  • B. Only one receiver is allowed, so there are not multiple consumers.
  • D. Channels are not network sockets.

Tip: Clone the Sender for each producer thread; one Receiver reads them all.

Question 9Hard

Rust's 'fearless concurrency' means:

  1. All code is run on a single thread by the compiler, so races cannot happen in the first place
  2. Threads never panic
  3. Deadlocks are impossible
  4. Data races are caught at compile time by ownership plus Send/Sync

Answer: D. Data races are caught at compile time by ownership plus Send/Sync

Explanation

Safe Rust has no data races, but logic bugs like deadlocks can still occur.

Step-by-step solution

  1. Ownership rules and the Send and Sync traits are checked at compile time.
  2. Together they reject code that would cause a data race.
  3. So data races are caught at compile time; option D is the correct answer.

Why the other options are wrong

  • A. Threads still run in parallel; the compiler does not make code single-threaded.
  • B. Threads can still panic.
  • C. Deadlocks can still happen; the compiler does not prevent them.

Tip: Rust prevents data races at compile time, but not deadlocks or logic bugs.

Question 10Hard

What happens to a Mutex in Rust if a thread panics while holding its lock?

  1. It is unlocked silently and nothing is reported
  2. It becomes poisoned; later lock() calls return Err
  3. It stays locked forever and blocks all threads
  4. The whole process aborts immediately

Answer: B. It becomes poisoned; later lock() calls return Err

Explanation

Poisoning signals that the data may be inconsistent; callers decide how to recover.

Step-by-step solution

  1. If a thread panics while holding a Mutex lock, the Mutex becomes poisoned.
  2. Later lock calls return Err, so the caller can decide how to recover.
  3. So option B is the correct answer.

Why the other options are wrong

  • A. Poisoning is reported through lock returning Err, so it is not silent.
  • C. The lock is not stuck forever; the poison flag lets callers respond.
  • D. The process does not abort; only the panicking thread stops.

Tip: Handle the Err from lock when a panic elsewhere could leave the data inconsistent.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics