Python From Beginner to Advanced

0% completed

Previous
Next
Quiz
hat is the main difference between threading and multiprocessing in Python?
A
Threading is for I/O-bound tasks, while multiprocessing is for CPU-bound tasks.
B
Multiprocessing is used to create software bugs more efficiently.
C
Threading cannot run tasks in parallel due to the GIL.
D
Multiprocessing is less efficient than threading in all scenarios.
How do you create a new process in Python using the multiprocessing module?
from multiprocessing import Process

def task():
    print("Task executed")

# Code to create and start a new process
A
Process.start(task)
B
p = Process(target=task); p.start()
C
p = Process(task); p.execute()
D
p = Process.run(task)
What does a Lock do in the context of threading?
A
Speeds up the execution of threads.
B
Ensures only one thread can enter the critical section of code at a time.
C
Prevents any thread from running.
D
Automatically terminates threads after a timeout.
Consider the following Python code. What synchronization issue might occur?
from threading import Thread, Lock

balance = 0
lock = Lock()

def deposit():
    global balance
    for _ in range(100000):
        lock.acquire()
        balance += 1
        lock.release()

t1 = Thread(target=deposit)
t2 = Thread(target=deposit)
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
A
The balance may not be 200000 due to a race condition.
B
The code will deadlock because locks are not properly released.
C
The balance will always correctly be 200000.
D
The Thread class does not support locking mechanisms.
What is asyncio in Python used for?
A
Managing system-level thread pools
B
Multiprocessing management
C
Real-time data processing in multiple threads
D
Handling asynchronous I/O operations using a single-threaded single-process approach
What would be the most likely approach to simulate thread priority in Python?
A
Use the priority attribute of the thread object.
B
Implement priority queues to manage execution order manually.
C
Utilize the multiprocessing module instead of threading.
D
Adjust the GIL settings to prioritize certain threads.
How do asynchronous generators work in Python?
async def async_generator():
    for i in range(5):
        yield i
        await asyncio.sleep(1)
A
They generate values synchronously without pausing.
B
They cannot use the await keyword within the body.
C
They yield values and can pause their execution with await.
D
This code will raise a SyntaxError.
In asynchronous programming with asyncio, what is the role of the event loop?
A
To execute asynchronous tasks sequentially.
B
To manage and distribute CPU resources among threads.
C
To schedule asynchronous tasks and manage their execution.
D
To encrypt data transmissions between asynchronous tasks.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next