Home »
MCQs
Concurrency with Python MCQs (Multiple-Choice Questions)
Concurrency in Python allows multiple tasks to make progress during overlapping periods of execution. Python provides several approaches to concurrency, including threads, processes, asynchronous programming with asyncio, and high-level executors provided by concurrent.futures.
Concurrency with Python MCQs
These Concurrency with Python multiple-choice questions cover important concepts such as threads, processes, the Global Interpreter Lock (GIL), locks, semaphores, events, queues, futures, thread pools, process pools, asynchronous programming, coroutines, event loops, and concurrent task execution. Practice these MCQs to test and improve your knowledge of Python concurrency.
List of Concurrency with Python MCQs
Here is a list of commonly asked Concurrency with Python MCQs with answers and explanations.
1. What is concurrency in Python?
- Executing only one instruction at a time
- Managing multiple tasks whose execution can overlap
- Compiling Python programs into machine code
- Storing multiple variables in memory
Answer: B) Managing multiple tasks whose execution can overlap
Explanation:
Concurrency allows multiple tasks to make progress during overlapping periods. Python provides several concurrency models, including threads, processes, and asynchronous programming.
2. Which Python module provides a high-level interface for working with threads and processes through executors?
concurrent.futures
thread.executor
parallel.python
async.executor
Answer: A) concurrent.futures
Explanation:
The concurrent.futures module provides a high-level interface for asynchronously executing callables using thread, process, and interpreter pools.
3. Which module is used to create and manage threads directly?
threading
multiprocessing
asyncio
concurrent.process
Answer: A) threading
Explanation:
The threading module provides a higher-level interface for creating and managing threads in Python.
4. Which class is used to create a thread with the threading module?
Thread
Task
Worker
ConcurrentThread
Answer: A) Thread
Explanation:
The threading.Thread class represents an activity that runs in a separate thread of execution.
5. Which method starts the execution of a Python thread?
run()
start()
execute()
begin()
Answer: B) start()
Explanation:
Calling start() arranges for the thread's run() method to be invoked in a separate thread of execution.
6. What does the join() method of a thread do?
- Creates another thread
- Waits for the thread to finish
- Terminates the thread immediately
- Pauses all Python programs
Answer: B) Waits for the thread to finish
Explanation:
The join() method blocks the calling thread until the target thread terminates or until an optional timeout expires.
7. Which parameter can be passed to threading.Thread to specify the function executed by the thread?
target
function
execute
call
Answer: A) target
Explanation:
The target argument specifies the callable that the thread should invoke when it starts.
8. Which argument is commonly used to pass positional arguments to a thread target?
args
parameters
values
arguments
Answer: A) args
Explanation:
The args argument can be used to provide positional arguments to the target callable of a thread.
9. Which argument is used to pass keyword arguments to a thread target?
kwargs
keywords
options
named
Answer: A) kwargs
Explanation:
The kwargs argument is used to pass keyword arguments to the callable executed by a thread.
10. What is the Global Interpreter Lock (GIL) in CPython?
- A database lock
- A mechanism that restricts execution of Python bytecode by multiple threads at the same time in standard CPython builds
- A file access lock
- A network security protocol
Answer: B) A mechanism that restricts execution of Python bytecode by multiple threads at the same time in standard CPython builds
Explanation:
In standard CPython builds, the GIL means that only one thread executes Python code at a time. This affects CPU-bound multithreaded programs, although threads remain useful for many I/O-bound workloads.
11. Which approach is generally suitable for CPU-bound work in standard CPython when true multi-core parallelism is required?
- Multiple threads only
- Multiple processes
- Only timers
- Only generators
Answer: B) Multiple processes
Explanation:
Processes have separate Python interpreter states and can execute Python code on multiple CPU cores independently. multiprocessing and ProcessPoolExecutor are common choices for CPU-bound work in standard CPython.
12. Which approach is often suitable for I/O-bound tasks such as waiting for network responses?
- Threading
- Only multiprocessing
- Only recursion
- Only sequential execution
Answer: A) Threading
Explanation:
Threads can be useful for I/O-bound tasks because a thread can wait for I/O while another thread makes progress.
13. Which module provides process-based parallelism?
multiprocessing
process.threading
parallelprocess
asyncprocess
Answer: A) multiprocessing
Explanation:
The multiprocessing module supports process-based concurrency and provides classes and utilities for creating and managing processes.
14. Which class is used to create a process with the multiprocessing module?
Process
Thread
WorkerProcess
TaskProcess
Answer: A) Process
Explanation:
multiprocessing.Process represents an activity that runs in a separate process.
15. Which method starts a multiprocessing Process?
start()
run_process()
execute()
begin()
Answer: A) start()
Explanation:
The start() method starts the child process and causes its target function to execute in that process.
16. Which method waits for a multiprocessing process to terminate?
join()
wait_process()
finish()
sync()
Answer: A) join()
Explanation:
The join() method allows the parent process to wait for a child process to finish.
17. Which class provides a pool of worker threads?
ThreadPoolExecutor
ThreadExecutorPool
WorkerThreadPool
ThreadGroup
Answer: A) ThreadPoolExecutor
Explanation:
ThreadPoolExecutor is an executor that uses a pool of threads to execute submitted callables asynchronously.
18. Which class provides a pool of worker processes?
ProcessPoolExecutor
ProcessExecutorPool
WorkerProcessPool
ParallelProcessExecutor
Answer: A) ProcessPoolExecutor
Explanation:
ProcessPoolExecutor uses a pool of separate processes to execute submitted callables asynchronously.
19. Which method submits a callable for asynchronous execution using an Executor?
submit()
execute()
schedule()
run_async()
Answer: A) submit()
Explanation:
The submit() method schedules a callable for execution and returns a Future representing its execution.
20. What does Executor.submit() return?
- A Thread
- A Future
- A Process
- A Queue
Answer: B) A Future
Explanation:
submit() returns a Future object that represents the asynchronous execution of the submitted callable.
21. Which method of a Future retrieves the result of the completed computation?
result()
get()
value()
output()
Answer: A) result()
Explanation:
The result() method returns the result of the callable represented by the Future. It waits if the result is not yet available, unless a timeout is specified.
22. What happens if the callable executed by a Future raises an exception?
- The exception is silently deleted
- The exception is raised when
result() retrieves the result
- The Python interpreter always terminates
- The Future is automatically converted to a thread
Answer: B) The exception is raised when result() retrieves the result
Explanation:
An exception raised by the callable is stored with the Future and is raised when its result is retrieved.
23. Which method can be used to cancel a Future that has not started running?
cancel()
stop()
terminate()
abort()
Answer: A) cancel()
Explanation:
The cancel() method attempts to cancel a Future. A task that has already started running cannot normally be cancelled through this method.
24. Which method checks whether a Future was successfully cancelled?
cancelled()
is_cancelled()
was_cancelled()
cancel_status()
Answer: A) cancelled()
Explanation:
The cancelled() method returns whether the Future was successfully cancelled before execution.
25. Which method checks whether a Future has completed or been cancelled?
done()
finished()
complete()
ready()
Answer: A) done()
Explanation:
The done() method returns True if the Future has completed or has been cancelled.
26. Which Executor method can apply a function to items from one or more iterables?
map()
apply()
parallel_map()
iterate()
Answer: A) map()
Explanation:
Executor.map() asynchronously executes a function over items from the supplied iterables and returns an iterator over the results.
27. Which method shuts down an Executor?
shutdown()
close()
terminate()
stop_executor()
Answer: A) shutdown()
Explanation:
The shutdown() method arranges for the executor to release resources after pending work has been completed, depending on its options.
28. Which Python statement can be used to manage an Executor automatically?
with
using
manage
executor
Answer: A) with
Explanation:
Executors support the context manager protocol, so using them with a with statement automatically performs shutdown when the block exits.
29. Which synchronization primitive prevents multiple threads from simultaneously entering a critical section?
- Lock
- Timer
- Event
- Queue
Answer: A) Lock
Explanation:
A Lock provides mutual exclusion, allowing one thread at a time to acquire the lock and enter a protected critical section.
30. Which method acquires a threading Lock?
acquire()
lock()
obtain()
enter()
Answer: A) acquire()
Explanation:
The acquire() method obtains the lock. If another thread already holds it, the call can wait until the lock becomes available.
31. Which method releases a threading Lock?
release()
unlock()
free()
exit()
Answer: A) release()
Explanation:
The release() method releases a previously acquired lock so another thread can acquire it.
32. What is a deadlock?
- A situation where tasks wait indefinitely for resources held by each other
- A thread that finishes normally
- A process that uses very little memory
- A successful synchronization operation
Answer: A) A situation where tasks wait indefinitely for resources held by each other
Explanation:
A deadlock can occur when concurrent tasks wait for locks or resources in a circular dependency, preventing the involved tasks from making progress.
33. Which synchronization primitive allows a limited number of threads to access a resource simultaneously?
- Semaphore
- Lock
- Event
- Timer
Answer: A) Semaphore
Explanation:
A semaphore maintains a counter representing available permits. It can be used to limit concurrent access to a resource.
34. Which synchronization primitive can notify one or more threads that an event has occurred?
- Event
- Lock
- Timer
- Barrier
Answer: A) Event
Explanation:
A threading.Event object manages an internal flag that threads can wait for and another thread can set.
35. Which synchronization primitive allows a group of threads to wait until a specified number of them have reached a common point?
- Barrier
- Semaphore
- Event
- Queue
Answer: A) Barrier
Explanation:
A Barrier allows a fixed number of threads to wait for each other before they continue execution.
36. Which module provides a thread-safe queue implementation?
queue
threadqueue
concurrent.queue
syncqueue
Answer: A) queue
Explanation:
The queue module provides synchronized queue classes that are designed for safely exchanging data between threads.
37. Which queue class provides a first-in, first-out ordering?
Queue
LIFOQueue
PriorityQueue
StackQueue
Answer: A) Queue
Explanation:
The standard queue.Queue class implements a first-in, first-out queue.
38. Which queue class retrieves items in last-in, first-out order?
LifoQueue
StackQueue
ReverseQueue
LastQueue
Answer: A) LifoQueue
Explanation:
LifoQueue implements a last-in, first-out queue, similar to stack behavior.
39. Which queue class retrieves entries based on priority?
PriorityQueue
PriorityList
OrderedQueue
PriorityStack
Answer: A) PriorityQueue
Explanation:
PriorityQueue retrieves entries in priority order rather than simple insertion order.
40. Which Python library is designed for asynchronous programming using async and await?
asyncio
async
awaitio
asyncthread
Answer: A) asyncio
Explanation:
asyncio is Python's library for writing concurrent code using the async and await syntax.
41. What is a coroutine in Python's asyncio programming model?
- An asynchronous function or coroutine object designed to be executed by the event loop
- A database connection
- A separate operating system process
- A synchronization lock
Answer: A) An asynchronous function or coroutine object designed to be executed by the event loop
Explanation:
Functions defined with async def produce coroutine objects when called. These can be scheduled and executed by the asyncio event loop.
42. Which keyword is used to define an asynchronous function?
async
await
concurrent
parallel
Answer: A) async
Explanation:
An asynchronous function is defined using async def. Such a function can use await to suspend its execution while waiting for another awaitable.
43. Which keyword is used to suspend a coroutine until an awaitable completes?
await
async
yield
pause
Answer: A) await
Explanation:
The await expression suspends the current coroutine until the supplied awaitable completes, allowing the event loop to run other tasks.
44. Which function is commonly used to run an asyncio coroutine as the main entry point?
asyncio.run()
asyncio.start()
asyncio.execute()
asyncio.main()
Answer: A) asyncio.run()
Explanation:
asyncio.run() is a high-level function used to execute an awaitable as the main entry point of an asyncio program.
45. What is the role of an asyncio event loop?
- It schedules and runs asynchronous tasks and callbacks
- It compiles Python source code
- It creates only operating system processes
- It stores database records
Answer: A) It schedules and runs asynchronous tasks and callbacks
Explanation:
The asyncio event loop manages and executes asynchronous tasks, callbacks, and I/O operations, switching between tasks when they are able to make progress.
46. Which function can schedule a coroutine as an asyncio Task?
asyncio.create_task()
asyncio.schedule_task()
asyncio.run_task()
asyncio.start_task()
Answer: A) asyncio.create_task()
Explanation:
asyncio.create_task() schedules the execution of a coroutine and returns an asyncio Task object.
47. Which asyncio function can wait for multiple awaitables to complete and return their results?
asyncio.gather()
asyncio.collect()
asyncio.join_all()
asyncio.wait_all()
Answer: A) asyncio.gather()
Explanation:
asyncio.gather() runs awaitables concurrently and returns their results after they complete, subject to its exception-handling behavior.
48. Which executor was introduced in Python 3.14 to execute tasks in separate interpreters?
InterpreterPoolExecutor
MultiInterpreterExecutor
SubInterpreterPool
InterpreterExecutor
Answer: A) InterpreterPoolExecutor
Explanation:
InterpreterPoolExecutor was added in Python 3.14. It uses a pool of threads where each worker runs in its own interpreter, providing interpreter isolation and true multi-core parallelism.
49. What is an important characteristic of ProcessPoolExecutor?
- Submitted functions and arguments generally need to be pickleable
- It only accepts strings
- It can execute only asynchronous functions
- It always shares all mutable objects directly between processes
Answer: A) Submitted functions and arguments generally need to be pickleable
Explanation:
ProcessPoolExecutor uses separate processes, so objects passed between the parent and worker processes generally need to be pickleable. Functions defined interactively or lambda functions should not be expected to work with it.
50. Which statement best describes concurrency in Python?
- Python supports only multithreading
- Python supports only multiprocessing
- Python provides multiple concurrency models, including threads, processes, executors, and asyncio
- Python cannot execute multiple tasks concurrently
Answer: C) Python provides multiple concurrency models, including threads, processes, executors, and asyncio
Explanation:
Python provides several concurrency mechanisms. Threads are useful for many I/O-bound workloads, processes can provide multi-core parallelism, concurrent.futures provides high-level executor interfaces, and asyncio supports asynchronous I/O and task-level concurrency.
Advertisement
Advertisement