AlgoMaster Logo

Concurrency - Quiz

Last Updated: December 6, 2025

1 min read

Concurrency Quiz

30 quizzes

1
Multiple Choice

Which statement about std::thread in C++ is TRUE?

2
Multiple Choice

What happens if a joinable std::thread object is destroyed?

3
Multiple Choice

How are arguments passed to a function run by std::thread?

4
Multiple Choice

Which use of detach() is MOST appropriate?

5
Multiple Choice

Which is the BEST way to protect shared data from race conditions?

6
Multiple Choice

What is a key advantage of std::lock_guard over manual mutex lock()/unlock()?

7
Multiple Choice

Which statement about std::unique_lock is TRUE?

8
Multiple Choice

What does std::condition_variable::wait(lock, pred) guarantee?

9
Multiple Choice

Which launch policy forces std::async to run the function on a new thread?

10
Multiple Choice

Which statement about thread_local variables is CORRECT?

11
Code Completion

Start a new thread that runs the function worker with an integer argument

cpp
1
std::thread t( worker, );

Click an option to fill the blank:

12
Code Completion

Use RAII to lock a mutex inside a function

cpp
1
std::lock_guard<std::mutex> guard( );

Click an option to fill the blank:

13
Code Completion

Declare an atomic counter of type long

cpp
1
std::<long> counter{0};

Click an option to fill the blank:

14
Output Prediction

What is the output of this code involving two threads incrementing a shared int without synchronization?

1#include <iostream>
2#include <thread>
3
4int counter = 0;
5
6void inc() {
7    for (int i = 0; i < 1000; ++i) {
8        ++counter;
9    }
10}
11
12int main() {
13    std::thread t1(inc);
14    std::thread t2(inc);
15    t1.join();
16    t2.join();
17    std::cout << counter << std::endl;
18    return 0;
19}
15
Output Prediction

What does this program print about joinable state?

1#include <iostream>
2#include <thread>
3
4void work() {}
5
6int main() {
7    std::thread t(work);
8    std::cout << std::boolalpha << t.joinable() << " ";
9    t.join();
10    std::cout << t.joinable() << std::endl;
11    return 0;
12}
16
Output Prediction

What does this code using std::async print?

1#include <iostream>
2#include <future>
3#include <thread>
4
5int compute(int x) {
6    std::this_thread::sleep_for(std::chrono::milliseconds(10));
7    return x * 2;
8}
9
10int main() {
11    auto fut = std::async(std::launch::async, compute, 5);
12    std::cout << "waiting";
13    int result = fut.get();
14    std::cout << " " << result << std::endl;
15    return 0;
16}
17
Output Prediction

What is the output regarding thread_local variable values?

1#include <iostream>
2#include <thread>
3
4thread_local int x = 0;
5
6void f() {
7    ++x;
8    std::cout << x << " ";
9}
10
11int main() {
12    std::thread t1(f);
13    std::thread t2(f);
14    t1.join();
15    t2.join();
16    f();
17    std::cout << std::endl;
18    return 0;
19}
18
Output Prediction

What does this atomic counter example print?

1#include <iostream>
2#include <atomic>
3#include <thread>
4
5std::atomic<int> counter{0};
6
7void inc() {
8    for (int i = 0; i < 1000; ++i) {
9        counter.fetch_add(1, std::memory_order_relaxed);
10    }
11}
12
13int main() {
14    std::thread t1(inc);
15    std::thread t2(inc);
16    t1.join();
17    t2.join();
18    std::cout << counter.load() << std::endl;
19    return 0;
20}
19
Bug Spotting

Find the bug in this code using std::thread

Click on the line(s) that contain the bug.

cpp
1
#include <thread>
2
 
3
void work();
4
 
5
void start() {
6
    std::thread t(work);
7
    // do something
8
} // end of function
9
 
20
Bug Spotting

Find the bug in this producer-consumer using condition_variable

Click on the line(s) that contain the bug.

cpp
1
#include <mutex>
2
#include <condition_variable>
3
#include <queue>
4
 
5
std::mutex mtx;
6
std::condition_variable cv;
7
std::queue<int> q;
8
 
9
void consumer() {
10
    std::unique_lock<std::mutex> lock(mtx);
11
    if (q.empty()) {
12
        cv.wait(lock);
13
    }
14
    int v = q.front();
15
    q.pop();
16
}
17
 
21
Hotspot Selection

Click the line that can cause a deadlock

Click on the line to select.

cpp
1
#include <mutex>
2
 
3
std::mutex m1;
4
std::mutex m2;
5
 
6
void func1() {
7
    std::lock_guard<std::mutex> lock1(m1);
8
    std::lock_guard<std::mutex> lock2(m2);
9
}
10
 
11
void func2() {
12
    std::lock_guard<std::mutex> lock2(m2);
13
    std::lock_guard<std::mutex> lock1(m1);
14
}
15
 
22
Hotspot Selection

Click the line where the shared data is NOT protected by the mutex

Click on the line to select.

cpp
1
#include <mutex>
2
#include <vector>
3
 
4
std::mutex mtx;
5
std::vector<int> data;
6
 
7
void add(int x) {
8
    std::lock_guard<std::mutex> lock(mtx);
9
    data.push_back(x);
10
}
11
 
12
int get_first() {
13
    if (data.empty()) return -1;
14
    return data[0];
15
}
16
 
23
Fill in the Blanks

Complete the correct use of std::ref to pass a reference to a thread

cpp
1
#include <thread>
2
#include <vector>
3
4
void append(std::vector<int>& v) {
5
v.push_back(1);
6
}
7
8
int main() {
9
std::vector<int> v;
10
std::thread t(append, (v));
11
t.join();
12
}
13

Click an option to fill blank 1:

24
Fill in the Blanks

Fill in the blanks to correctly use condition_variable with a predicate

cpp
1
#include <mutex>
2
#include <condition_variable>
3
4
std::mutex mtx;
5
std::condition_variable cv;
6
bool ready = false;
7
8
void wait_for_ready() {
9
std::unique_lock<std::mutex> lock(mtx);
10
cv.(lock, [] { return ; });
11
}
12

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the code to declare and use a future returned from std::async

cpp
1
#include <future>
2
3
int compute() { return 7; }
4
5
int main() {
6
auto fut = std::async(std::launch::async, compute);
7
int value = fut.();
8
<< value << std::endl;
9
}
10

Click an option to fill blank 1:

26
Fill in the Blanks

Fill in the blanks to declare a thread_local variable and increment it

cpp
1
#include <thread>
2
#include <iostream>
3
4
int counter = 0;
5
6
void inc() {
7
++counter;
8
std::cout << counter << std::endl;
9
}
10

Click an option to fill blank 1:

27
Matching

Match each concurrency primitive to its main use

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match async mechanisms with how results are obtained

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the typical steps to safely consume items from a shared queue using a mutex and condition_variable

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps to use std::promise and std::future to compute a value in a background thread

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.