Lec1 Threads
Lec1 Threads
B) A program in execution
3. In a typical video game scenario, what might be the purpose of using multiple threads?
5. Which statement accurately describes the relationship between threads and processes?
A) Kernel thread
B) User thread
C) Primary thread
D) Secondary thread
8. What advantage do multithreaded programs offer on computer systems with multiple CPUs?
B) Increased responsiveness
A) multiprocessing
B) concurrent
C) thread
D) threading
11. What function from the _thread module is used to spawn a new thread in Python?
A) create_thread()
B) start_thread()
C) start_new_thread()
D) spawn_thread()
12. How does the start_new_thread function behave when called in Python?
A) It waits for the main thread to finish execution
13. What are the arguments required by the start_new_thread function in Python?
14. What is the purpose of the kwargs parameter in the start_new_thread function?
15. Which module provides an efficient way to create new threads in Python?
A) multiprocessing
B) concurrent
C) threading
D) _thread
A) multiprocessing
B) concurrent
C) thread
D) threading
17. In Python, what is the difference between the _thread and threading modules?
A) Kernel threads are managed by the operating system, while user threads are not.
19. What does the term "concurrency" refer to in the context of multithreading?
A) Function
B) Class
C) Module
D) Object
ANSWERS:
2. B) A program in execution
7. A) Kernel thread
8. B) Increased responsiveness
10. D) threading
11. C) start_new_thread()
15. D) threading
16. D) threading
18. A) Kernel threads are managed by the operating system, while user threads are not.
20. A) Function
MCQ
1. What controls Python execution?
C) Threading module
E) CPU Scheduler
A) Unlimited
B) Two
C) One
D) Three
A) multiprocessing
B) concurrent.futures
C) threading
D) os
E) sys
6. What happens to child threads when the main thread ends using the `thread` module?
A) multiprocessing
B) concurrent.futures
C) thread
D) threading
E) multiprocessing.dummy
8. What is the behavior of the `thread` module when the main thread ends?
C) It raises an error
D) It depends on the implementation
9. Which of the following statements regarding the Global Interpreter Lock (GIL) is true?
10. What is the primary limitation of using the `thread` module for threading in Python?
C) It does not provide sufficient control over thread creation and termination.
12. Which module is recommended for creating and managing threads in Python?
A) os
B) multiprocessing
C) thread
D) concurrent
E) threading
13. What is the purpose of the Global Interpreter Lock (GIL) in Python?
14. How does the `threading` module differ from the `thread` module in Python?
C) The `threading` module allows for finer control over the Global Interpreter Lock.
15. What happens to child threads when the main thread terminates in Python?
16. Which module should be used for thread management if you require a high level of control over
threading in Python?
A) thread
B) multiprocessing
C) concurrent.futures
D) threading
E) subprocess
17. What is the main limitation of the Global Interpreter Lock (GIL) in Python?
18. Which of the following statements about the threading module in Python is true?
A) It provides lower-level control over threads compared to the thread module.
B) It automatically releases the Global Interpreter Lock when a thread enters a critical section.
19. What is the purpose of the Global Interpreter Lock (GIL) in Python's threading model?
20. Which module should be preferred for thread management in Python due to its higher-level
functionalities?
A) thread
B) multiprocessing
C) concurrent.futures
D) threading
E) os
ANSWERS:
Certainly, here are the answers to the multiple-choice questions:
2. C) One
5. C) threading
7. D) threading
8. C) It raises an error
9. D) It prevents multiple threads from executing Python bytecode simultaneously.
10. C) It does not provide sufficient control over thread creation and termination.
12. E) threading
16. D) threading
18. A) It provides lower-level control over threads compared to the thread module.
20. D) threading
#####################################################################################
#####################################################################################
b) A program in execution
c) A block of memory
d) An input/output operation
a) Local variables
b) Address space
c) Memory stack
5. Which of the following tasks might require the use of multiple threads?
a) Printing a document
c) Reading a file
d) Deleting a folder
a) A lightweight process
b) A separate program
c) A hardware component
d) A network protocol
8. In the context of a video game, which task might require a separate thread?
a) Loading textures
c) Rendering graphics
c) Improved responsiveness
12. Which of the following is an example of a task that might require multiple threads?
a) Sorting an array
b) Playing music
14. In the context of a browser, what might be a task handled by a separate thread?
a) Displaying images
b) Parsing HTML
d) Managing bookmarks
a) Sequential tasks
b) CPU-bound tasks
c) I/O-bound tasks
d) Memory-bound tasks
c) To improve responsiveness
1. b) A program in execution
4. b) Address space
6. a) A lightweight process
8. a) Loading textures
13. d) They can use the same variables and data structures
a) `thread.run_thread()`
b) `thread.execute_thread()`
c) `start_new_thread()`
d) `create_thread()`
5. What does the `start_new_thread()` function return immediately after invocation?
d) None
a) `start()`
b) `begin()`
c) `run()`
d) `execute()`
15. What is the main benefit of using the threading module over the _thread module in Python?
a) Better performance
b) Easier syntax
c) Higher-level abstractions
19. How does threading facilitate the execution of multiple tasks simultaneously?
4. c) `start_new_thread()`
5. d) None
b) Spawns a new thread and executes the given function with specified arguments
c) Releases a lock
d) Acquires a lock
b) Releases a lock
d) Acquires a lock
5. What is the purpose of the `acquire()` method of a lock object?
b) Releases a lock
b) When it terminates
a) To terminate threads
d) To release threads
8. What is the implication of Python's Global Interpreter Lock (GIL) on multiprocessor systems?
10. What is the purpose of the Global Interpreter Lock (GIL) in Python?
11. What happens if the main thread in Python doesn't sleep longer than the child threads?
c) The program exits when the main function finishes, regardless of the child threads
12. What does the Python Virtual Machine control in Python execution?
13. How many threads can execute in the Python interpreter at a time?
a) Only one
16. Why should one be cautious when using low-level thread control functions like `thread.exit()`?
a) They are highly efficient and recommended for all threading scenarios
b) They may lead to unexpected program behavior and should be used with caution
17. What is the primary reason for using locks in multithreaded programming?
b) To ensure that only one thread can access a shared resource at a time
c) To increase the number of threads in the program
a) Unlocked
b) Locked
c) Waiting
d) Terminated
a) `threading`
b) `_thread`
c) `concurrent.futures`
d) `multiprocessing`
20. What is the significance of the Global Interpreter Lock (GIL) in Python?
1. b) Spawns a new thread and executes the given function with specified arguments
6. b) When it terminates
16. b) They may lead to unexpected program behavior and should be used with caution
17. b) To ensure that only one thread can access a shared resource at a time
18. a) Unlocked
19. a) `threading`
- A. By running slower.
- C. They allow the program to do something else while one thread is waiting for an I/O task.
8. **Which advantage of multithreaded programs is true for both single and multiple CPU systems?**
1. A
2. C
3. B
4. D
5. D
6. C
7. C
8. B
9. B
10. B