Python Concurrency, Asynchronous Programming, and GIL Fundamentals Quiz

Test your understanding of key Python interview topics, including concurrency, asynchronous execution, and the Global Interpreter Lock (GIL). This quiz helps you quickly review essential Python concepts relevant for technical interviews.

  1. Concurrency - Concept

    Which of the following best describes concurrency in Python with an example?

    1. Managing multiple tasks that appear to execute together, like a chef switching between several dishes.
    2. Running multiple tasks strictly one after the other, not overlapping at all.
    3. Performing only one task at a time until completion, such as reading one book before starting another.
    4. Starting a single task and blocking all others until it finishes.

    Explanation: Concurrency is about running multiple tasks in overlapping time periods, even if not truly simultaneously, much like a chef preparing different dishes by switching back and forth. The second and fourth options describe strictly sequential execution, not concurrency. The third option refers to blocking behavior, which is the opposite of concurrency.

  2. Asynchronous Execution

    In Python, what is the key feature of asynchronous execution using async/await?

    1. Tasks always run simultaneously on separate CPUs.
    2. Non-blocking execution allows tasks to pause and let others run in a single thread.
    3. Each asynchronous task requires a new system process.
    4. Only one task can be processed from start to finish before moving to the next.

    Explanation: Asynchronous execution in Python uses an event loop and async/await to allow tasks to pause and resume, enabling non-blocking behavior in a single thread. The second option incorrectly describes parallelism, not asynchrony. The third option confuses asynchronous execution with multiprocessing. The fourth option is the opposite of asynchronous execution.

  3. GIL—Definition

    What is the Global Interpreter Lock (GIL) in Python?

    1. A feature that only affects asynchronous programming, not threading.
    2. A tool used for managing file operations in Python.
    3. A lock that prevents any type of parallelism in Python, including processes.
    4. A mechanism that ensures only one thread executes Python bytecode at a time.

    Explanation: The GIL ensures only one native thread can execute Python bytecode at a time in CPython. It doesn't prevent parallelism via separate processes, which makes option two wrong. Option three is incorrect because GIL impacts threads, not async code. The last option has nothing to do with GIL.

  4. Concurrency - Python Implementation

    Which Python module is typically used for concurrency with I/O-bound tasks?

    1. format
    2. selct
    3. threading
    4. random

    Explanation: The 'threading' module is commonly used to manage concurrency for I/O-bound tasks in Python. 'random' is used for generating random numbers and is unrelated. 'selct' is a misspelling of 'select', which is useful for low-level I/O multiplexing but not typically used for threading. 'format' is used for formatting strings.

  5. Multiprocessing Purpose

    In Python, when should you prefer multiprocessing over multithreading?

    1. When changing the style of code outputs.
    2. When reading from disk with delays.
    3. When tasks are CPU-bound, like heavy calculations.
    4. When all tasks are waiting for user input.

    Explanation: Multiprocessing is preferable for CPU-bound tasks because it bypasses the GIL; each process has its own Python interpreter and memory space. Multithreading works better for I/O-bound tasks, not CPU-bound ones. Tasks involving waiting for user input or formatting outputs do not require multiprocessing.

  6. Asynchronous Use Case

    What type of task would benefit most from Python's async programming with async/await?

    1. Calculating the square root of a number.
    2. Sorting a large list in memory.
    3. Making many simultaneous API calls.
    4. Computing factorials of large numbers repeatedly.

    Explanation: Async programming excels at handling many I/O-bound tasks, like making multiple API calls, without blocking operations. The other three options are CPU-bound computations and would not benefit from async/await; those cases are better handled by multiprocessing if needed.

  7. Event Loop Function

    What is the purpose of the event loop in asynchronous Python programming?

    1. To manage and schedule coroutines, switching between tasks during waits.
    2. To automatically format and print program output.
    3. To restrict the number of running processes to one.
    4. To lock resources and prevent all but one thread from working.

    Explanation: The event loop coordinates and schedules asynchronous tasks, allowing them to pause and resume efficiently. The second and third options refer to locking mechanisms and process limitations, not event loops. The fourth option is irrelevant to the event loop's function.

  8. GIL Impact

    How does the GIL affect multi-threaded CPU-bound programs in Python?

    1. It has no effect on programs that calculate numbers.
    2. It makes all threads run faster by providing more memory.
    3. It prevents threads from running in true parallel, causing potential performance loss.
    4. It always increases execution speed by managing resources strictly.

    Explanation: The GIL restricts CPU-bound threads from running in parallel, impacting their performance negatively. The second option is incorrect; GIL doesn't speed up programs or offer more memory. The third option is false since GIL directly impacts CPU-bound programs. The last option is a misconception, as strict GIL management can slow down execution.

  9. I/O-bound Task Example

    Which of the following is an example of an I/O-bound task that benefits from multithreading or async programming in Python?

    1. Calculating thousands of digits of pi.
    2. Sorting huge datasets in memory.
    3. Downloading several files from the internet at once.
    4. Multiplying two numbers.

    Explanation: Downloading files involves waiting for network operations, an I/O-bound task ideal for multithreading or async programming. The other options are CPU-bound computations, which do not benefit from these approaches due to the GIL's constraints.

  10. True Parallelism in Python

    Which technique allows for true parallel execution of Python code for CPU-intensive tasks?

    1. Using multiprocessing to leverage multiple processes.
    2. Creating multiple threads in a single process.
    3. Running all tasks using the format() function.
    4. Writing loops with async and await only.

    Explanation: Multiprocessing starts separate Python processes, enabling true parallel execution and bypassing the GIL. The second option, format(), is unrelated. The third option only allows non-blocking I/O but does not achieve parallel CPU computation. The fourth option is limited by the GIL and does not deliver true parallelism for CPU-bound tasks.