AlgoMaster Logo

Concurrency - Quiz

Last Updated: December 6, 2025

Concurrency Exercises

28 quizzes

1
Code Completion

Complete the expression so it creates a Thread object that will run the function handle_request.

python
1
worker = threading.Thread(target=)

Click an option to fill the blank:

2
Code Completion

Complete the expression so it safely acquires a lock using a context manager before updating a shared counter.

python
1
with counter_lock.: shared_counter += 1

Click an option to fill the blank:

3
Code Completion

Complete the expression so it schedules the coroutine fetch_page(url) as a new asyncio Task.

python
1
task = asyncio.(fetch_page(url))

Click an option to fill the blank:

4
Multiple Choice

Which scenario benefits most from using threads instead of running tasks sequentially?

5
Multiple Choice

In Python's threading module, what does thread.join() do?

6
Multiple Choice

What is the main purpose of a threading.Lock?

7
Multiple Choice

Which condition is required for a deadlock to occur?

8
Multiple Choice

Why can the multiprocessing module bypass the limitations of the GIL for CPU-bound work?

9
Multiple Choice

What is a key difference between ThreadPoolExecutor and ProcessPoolExecutor?

10
Multiple Choice

In asyncio, what does the 'await' keyword do?

11
Multiple Choice

What is the role of the asyncio event loop?

12
Multiple Choice

What does asyncio.create_task() return?

13
Multiple Choice

Which statement about the Global Interpreter Lock (GIL) in CPython is correct?

14
Sequencing

Order the steps to safely increment a shared counter from multiple threads using a Lock.

Drag and drop to reorder, or use the arrows.

15
Sequencing

Order the steps to concurrently fetch two URLs using asyncio coroutines.

Drag and drop to reorder, or use the arrows.

16
Output Prediction

What is the output of this code using a simple Lock for synchronization?

1import threading
2
3value = 0
4lock = threading.Lock()
5
6with lock:
7    value += 5
8
9print(value)
17
Output Prediction

What is the output when using concurrent.futures with a ProcessPoolExecutor?

1from concurrent.futures import ProcessPoolExecutor
2
3def square(x):
4    return x * x
5
6with ProcessPoolExecutor(max_workers=2) as executor:
7    future = executor.submit(square, 4)
8
9print(future.result())
18
Output Prediction

What is printed when running this asyncio code?

1import asyncio
2
3async def greet():
4    await asyncio.sleep(0)
5    return "hello"
6
7result = asyncio.run(greet())
8print(result.upper())
19
Output Prediction

What is the output of this code illustrating the GIL's effect on simple threading?

1import threading
2
3counter = 0
4
5def add_one():
6    global counter
7    counter += 1
8
9thread = threading.Thread(target=add_one)
10thread.start()
11thread.join()
12
13print(counter)
20
Output Prediction

What will this multiprocessing example print?

1import multiprocessing
2
3def get_name():
4    return multiprocessing.current_process().name
5
6if __name__ == "__main__":
7    with multiprocessing.Pool(processes=1) as pool:
8        name = pool.apply(get_name)
9    print(isinstance(name, str))
21
Bug Spotting

Find the bug in this code that tries to protect a shared counter with a Lock.

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

python
1
import threading
2
 
3
shared_counter = 0
4
lock = threading.Lock()
5
 
6
def increment_many(times):
7
    global shared_counter
8
    for _ in range(times):
9
        lock.acquire
10
        shared_counter += 1
11
        lock.release()
12
 
22
Bug Spotting

Find the bug in this asyncio code that creates tasks for coroutines.

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

python
1
import asyncio
2
 
3
async def do_work():
4
    await asyncio.sleep(1)
5
    return "done"
6
 
7
async def main():
8
    task = asyncio.create_task(do_work)
9
    result = await task
10
    print(result)
11
 
12
asyncio.run(main())
13
 
23
Matching

Match each concurrency tool with its best description.

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

24
Matching

Match each concept with the concurrency model it belongs to.

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

25
Fill in the Blanks

Complete the code to use a Process Pool for CPU-bound work and collect all results.

python
1
import multiprocessing
2
3
def compute_square(number):
4
return number * number
5
6
if __name__ == "__main__":
7
with multiprocessing.(processes=4) as pool:
8
results = pool.(compute_square, range(5))
9
print(list(results))
10

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the asyncio code so two coroutines run concurrently and both results are printed.

python
1
import asyncio
2
3
async def fetch_data(delay):
4
await asyncio.sleep(delay)
5
return f"waited {delay}"
6
7
async def main():
8
task_one = asyncio.(fetch_data(1))
9
task_two = asyncio.(fetch_data(2))
10
result_one, result_two = await asyncio.(task_one, task_two)
11
print(result_one, result_two)
12
13
asyncio.run(main())
14

Click an option to fill blank 1:

27
Hotspot Selection

Click the line that can cause a deadlock when two threads run acquire_locks() concurrently.

Click on the line to select.

python
1
import threading
2
 
3
lock_a = threading.Lock()
4
lock_b = threading.Lock()
5
 
6
def acquire_locks():
7
    with lock_a:
8
        with lock_b:
9
            print("Both locks acquired")
10
 
11
 
28
Hotspot Selection

Click the line where using blocking I/O inside an async function can block the event loop.

Click on the line to select.

python
1
import asyncio
2
import time
3
 
4
async def slow_operation():
5
    time.sleep(2)
6
    return "done"
7
 
8
async def main():
9
    result = await slow_operation()
10
    print(result)
11
 
12
asyncio.run(main())
13
 

Premium Content

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