Rust Concurrency Quiz

Rust
0 Passed
0% acceptance

Master Rust's concurrency model with threads, channels, and synchronization primitives for safe parallel programming

40 Questions
~80 minutes
1

Question 1

What is the primary concurrency model in Rust?

A
Message passing with channels
B
Shared memory with locks
C
Actor model
D
CSP (Communicating Sequential Processes)
2

Question 2

What does this basic thread example do?

rust
use std::thread;
let handle = thread::spawn(|| {
    println!("Hello from thread!");
});
handle.join().unwrap();
A
Creates new thread, executes closure, waits for completion
B
Compilation error
C
Creates thread but doesn't wait
D
Runtime error
3

Question 3

What is the difference between thread::spawn and thread::Builder?

A
Builder allows customizing thread properties like name and stack size
B
Builder creates threads differently
C
They are identical
D
Builder doesn't exist
4

Question 4

What does move closure do in thread::spawn?

A
Transfers ownership of captured variables to new thread
B
Copies variables to new thread
C
Shares variables between threads
D
Move is not needed
5

Question 5

What does this channel example demonstrate?

rust
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
    tx.send(42).unwrap();
});
let received = rx.recv().unwrap();
A
Message passing between threads using channels
B
Compilation error
C
Shared memory access
D
Runtime error
6

Question 6

What is mpsc in std::sync::mpsc?

A
Multiple Producer, Single Consumer
B
Multiple Producer, Single Consumer
C
Message Passing System
D
Mutex Protected Shared Channel
7

Question 7

What does recv() return?

A
Result<T, RecvError> - blocks until message received
B
Option<T> - returns None if no message
C
T directly
D
recv() doesn't exist
8

Question 8

What does try_recv() do?

A
Non-blocking receive, returns immediately
B
Blocks like recv()
C
Sends message
D
try_recv() doesn't exist
9

Question 9

What is a Mutex in Rust?

A
Mutual exclusion primitive for shared mutable state
B
Channel for message passing
C
Thread creation primitive
D
Mutex doesn't exist
10

Question 10

What does this Mutex example show?

rust
use std::sync::Mutex;
let m = Mutex::new(5);
{
    let mut num = m.lock().unwrap();
    *num = 6;
}
println!("m = {:?}", m);
A
Thread-safe mutation of shared data
B
Compilation error
C
Data race
D
Runtime panic
11

Question 11

What happens if you try to lock a Mutex from the same thread twice?

A
Deadlock - thread waits for itself
B
Compilation error
C
Runtime panic
D
Succeeds
12

Question 12

What is RwLock?

A
Reader-writer lock allowing multiple readers or single writer
B
Read-only lock
C
Write-only lock
D
RwLock doesn't exist
13

Question 13

What does Arc stand for in concurrent contexts?

A
Atomic Reference Counting
B
Asynchronous Reference Counting
C
Automatic Reference Counting
D
Arc doesn't exist
14

Question 14

What does this Arc<Mutex<T>> example show?

rust
use std::sync::{Arc, Mutex};
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
    let counter = Arc::clone(&counter);
    let handle = thread::spawn(move || {
        let mut num = counter.lock().unwrap();
        *num += 1;
    });
    handles.push(handle);
}
for handle in handles {
    handle.join().unwrap();
}
A
Thread-safe shared mutable counter using Arc<Mutex<>>
B
Compilation error
C
Data race
D
Deadlock
15

Question 15

What are the Send and Sync traits?

A
Send allows transfer between threads, Sync allows shared access
B
Send allows sharing, Sync allows transfer
C
They are the same
D
These traits don't exist
16

Question 16

Which types are not Send?

A
Rc<T> (not thread-safe), raw pointers, RefCell<T>
B
Arc<T>, Mutex<T>
C
All types are Send
D
None are Send
17

Question 17

Which types are not Sync?

A
Cell<T>, RefCell<T>, raw pointers
B
Arc<T>, Mutex<T>
C
All types are Sync
D
None are Sync
18

Question 18

What is a data race?

A
Concurrent access to same memory with at least one write
B
Multiple threads reading same data
C
Sequential access to data
D
Data races don't exist in Rust
19

Question 19

What is a race condition?

A
Program behavior depends on timing of operations
B
Compilation error
C
Same as data race
D
Race conditions don't exist
20

Question 20

What does thread::park() do?

A
Puts current thread to sleep until unparked
B
Creates new thread
C
Terminates thread
D
park() doesn't exist
21

Question 21

What is thread::yield_now()?

A
Hints scheduler to switch to another thread
B
Terminates current thread
C
Blocks indefinitely
D
yield_now() doesn't exist
22

Question 22

What is a barrier?

A
Synchronization point where threads wait for each other
B
Memory barrier
C
Channel type
D
Barriers don't exist
23

Question 23

What does this barrier example do?

rust
use std::sync::Barrier;
let barrier = Arc::new(Barrier::new(2));
let b1 = barrier.clone();
let handle = thread::spawn(move || {
    println!("Before barrier");
    b1.wait();
    println!("After barrier");
});
barrier.wait();
handle.join().unwrap();
A
Synchronizes two threads at barrier point
B
Compilation error
C
Race condition
D
Deadlock
24

Question 24

What is a Condvar (condition variable)?

A
Allows threads to wait for specific condition to become true
B
Variable that changes automatically
C
Channel type
D
Condvar doesn't exist
25

Question 25

What is the difference between notify_one() and notify_all()?

A
notify_one wakes one waiting thread, notify_all wakes all
B
They are identical
C
notify_one doesn't exist
D
notify_all wakes none
26

Question 26

What is thread local storage?

A
Data local to each thread, not shared between threads
B
Shared data across threads
C
Global variables
D
Thread local storage doesn't exist
27

Question 27

What does this thread_local example show?

rust
use std::cell::RefCell;
thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
FOO.with(|f| {
    *f.borrow_mut() = 2;
});
let handle = thread::spawn(|| {
    FOO.with(|f| {
        println!("Other thread: {}", *f.borrow());
    });
});
handle.join().unwrap();
A
Each thread has separate copy of FOO
B
Compilation error
C
Shared mutable state
D
Data race
28

Question 28

What is the scoped thread API?

A
Threads that borrow from parent scope safely
B
Global threads
C
Threads with unlimited lifetime
D
Scoped threads don't exist
29

Question 29

What is the main advantage of message passing over shared state?

A
No data races, easier reasoning about concurrent code
B
Better performance
C
Less code
D
No advantage
30

Question 30

What is the main advantage of shared state over message passing?

A
Lower overhead for frequent small updates
B
No data races
C
Easier to reason about
D
No advantage
31

Question 31

What is deadlock?

A
Threads waiting for each other to release resources
B
Thread termination
C
High CPU usage
D
Deadlock doesn't exist
32

Question 32

How can you prevent deadlock?

A
Always acquire locks in same order, use timeouts
B
Use more threads
C
Use channels instead of mutexes
D
Deadlock cannot be prevented
33

Question 33

What is lock contention?

A
Threads competing for same lock, hurting performance
B
Multiple locks
C
No locks
D
Lock contention doesn't exist
34

Question 34

What is false sharing?

A
Cache line contention when threads modify nearby data
B
Incorrect sharing
C
No sharing
D
False sharing doesn't exist
35

Question 35

What is the difference between std::thread and rayon?

A
rayon provides data parallelism, std::thread provides task parallelism
B
They are identical
C
std::thread is better
D
rayon doesn't exist
36

Question 36

What does this rayon example do?

rust
use rayon::prelude::*;
let sum: i32 = (0..1000).into_par_iter().map(|x| x * x).sum();
A
Parallel computation of sum of squares
B
Compilation error
C
Sequential computation
D
Runtime error
37

Question 37

What is the key difference between concurrency and parallelism?

A
Concurrency is about dealing with multiple tasks, parallelism is about executing them simultaneously
B
They are the same
C
Concurrency is faster
D
Parallelism doesn't exist
38

Question 38

What is the thread pool pattern?

A
Pre-allocated threads waiting for work
B
Single thread handling all work
C
No threads
D
Thread pool doesn't exist
39

Question 39

What is cooperative vs preemptive scheduling?

A
Cooperative yields control voluntarily, preemptive can interrupt
B
They are identical
C
Cooperative is better
D
Preemptive doesn't exist
40

Question 40

In a web server handling multiple concurrent requests, each needing to update shared user session data and log to a shared file, which concurrency approach would you choose and why?

A
Message passing for logging, Arc<Mutex<>> for session data
B
All shared state with mutexes
C
All message passing
D
No concurrency needed

QUIZZES IN Rust