Any2Quiz
Computer Science / Languages / Rust

Concurrency Quiz - Languages Rust

Free practice quiz: Concurrency (Languages Rust). 55 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 55 questions free

Sample questions

  1. What fundamental primitive does Rust use to manage shared mutable state across threads without data races?

    1. Mutex
    2. RwLock
    3. RefCell
    4. Atomic types
    Show answer

    Answer: A. Mutex

    Rust's `std::sync::Mutex` is the primary primitive for protecting shared mutable data. It ensures that only one thread can access the data at a time, preventing data races. `RwLock` allows multiple readers or one writer. `RefCell` is for interior mutability but is not thread-safe. Atomic types are for simple, lock-free operations on primitive types.

  2. What is the primary mechanism Rust's `Mutex` uses to ensure mutual exclusion?

    1. Operating system primitives (e.g., futex on Linux)
    2. Busy-waiting loops
    3. Spinlocks
    4. Signal handling
    Show answer

    Answer: A. Operating system primitives (e.g., futex on Linux)

    Rust's standard library `Mutex` typically relies on underlying operating system primitives (like `futex` on Linux, `Windows Mutex` on Windows) to efficiently manage thread blocking and waking. This is more efficient than pure busy-waiting or spinlocks, which consume CPU cycles unnecessarily.

  3. Which method is used to create a channel that supports multiple producers and multiple consumers in Rust?

    1. `crossbeam_channel::unbounded()`
    2. `std::sync::mpsc::channel()`
    3. `std::sync::mpsc::sync_channel()`
    4. `std::thread::spawn()`
    Show answer

    Answer: A. `crossbeam_channel::unbounded()`

    While `std::sync::mpsc` is Multiple Producer Single Consumer, the `crossbeam-channel` crate provides more flexible channel types. `crossbeam_channel::unbounded()` can create channels that support multiple producers and multiple consumers. `std::sync::mpsc::channel()` is MPSC, and `sync_channel` has a bound on the number of messages.

  4. What does the `TryLockError` indicate when returned by a `try_lock()` method on a `Mutex` or `RwLock`?

    1. The lock could not be acquired immediately because another thread holds it.
    2. The lock was successfully acquired.
    3. The `Mutex` or `RwLock` is poisoned.
    4. The thread panicked while holding the lock.
    Show answer

    Answer: A. The lock could not be acquired immediately because another thread holds it.

    `try_lock()` attempts to acquire a lock without blocking. If the lock is already held by another thread, it returns a `TryLockError` (or similar) indicating that the lock is currently unavailable. It does not imply poisoning or successful acquisition.

  5. What is the purpose of the `SendSync` type alias in the `atomix` crate (or similar concepts)?

    1. To represent types that are both `Send` and `Sync`.
    2. To represent types that are only `Send`.
    3. To represent types that are only `Sync`.
    4. To represent types that are neither `Send` nor `Sync`.
    Show answer

    Answer: A. To represent types that are both `Send` and `Sync`.

    A `SendSync` type alias or trait bound is a common convention in Rust concurrency to explicitly state that a type must be safe to send to another thread (`Send`) and safe to share via immutable references across threads (`Sync`).

Take the full quiz

More quizzes

Browse all Rust quizzes