PYTHON Multithreading and Multiprocessing MCQs

  1. Which module is used for multithreading in Python?
    a) threading
    b) multiprocessing
    c) concurrent
    d) os
    View Answer

    Correct answer: A — threading

  2. What will be printed?
    1 python
    2 import threading
    3 print(hasattr(threading,"Thread"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  3. Which method starts a thread?
    a) start()
    b) run()
    c) begin()
    d) execute()
    View Answer

    Correct answer: A — start()

  4. What is the output of below code?
    1 python
    2 import threading
    3 def show():
    4 print("Eduinq")
    5 t = threading.Thread(target=show)
    6 t.start()
    a) Eduinq
    b) Error
    c) None
    d) Thread
    View Answer

    Correct answer: A — Eduinq

  5. Which method waits for thread completion?
    a) join()
    b) wait()
    c) stop()
    d) end()
    View Answer

    Correct answer: A — join()

  6. What will be printed?
    1 python
    2 import threading
    3 def show():
    4 print("Eduinq")
    5 t = threading.Thread(target=show)
    6 t.start()
    7 t.join()
    8 print("Done")
    a) Eduinq Done
    b) Done Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq Done

  7. Which of the following is used for synchronization?
    a) Lock
    b) Semaphore
    c) Event
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  8. What is the output of below code?
    1 python
    2 import threading
    3 lock = threading.Lock()
    4 print(isinstance(lock,threading.Lock))
    a) False
    b) True
    c) Error
    d) None
    View Answer

    Correct answer: A — False

  9. Which of the following is used for multiprocessing in Python?
    a) multiprocessing
    b) threading
    c) concurrent
    d) os
    View Answer

    Correct answer: A — multiprocessing

  10. What will be printed?
    1 python
    2 import multiprocessing
    3 print(hasattr(multiprocessing,"Process"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  11. Which method starts a process?
    a) start()
    b) run()
    c) begin()
    d) execute()
    View Answer

    Correct answer: A — start()

  12. What is the output of below code?
    1 python
    2 import multiprocessing
    3 def show():
    4 print("Eduinq")
    5 p = multiprocessing.Process(target=show)
    6 p.start()
    7 p.join()
    a) Eduinq
    b) Error
    c) None
    d) Process
    View Answer

    Correct answer: A — Eduinq

  13. Which of the following is used for inter process communication?
    a) Queue
    b) Pipe
    c) Manager
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  14. What will be printed?
    1 python
    2 import multiprocessing
    3 q = multiprocessing.Queue()
    4 q.put("Eduinq")
    5 print(q.get())
    a) Eduinq
    b) Error
    c) None
    d) Queue
    View Answer

    Correct answer: A — Eduinq

  15. Which of the following is used for shared memory?
    a) Value
    b) Array
    c) Manager
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  16. What is the output of below code?
    1 python
    2 import multiprocessing
    3 val = multiprocessing.Value('i',10)
    4 print(val.value)
    a) 10
    b) Error
    c) None
    d) i
    View Answer

    Correct answer: A — 10

  17. Which of the following is used for process pool?
    a) Pool
    b) ThreadPool
    c) ProcessGroup
    d) None
    View Answer

    Correct answer: A — Pool

  18. What will be printed?
    1 python
    2 import multiprocessing
    3 pool = multiprocessing.Pool(processes=2)
    4 print(hasattr(pool,"map"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  19. Which of the following is used for concurrent futures?
    a) concurrent.futures
    b) threading
    c) multiprocessing
    d) os
    View Answer

    Correct answer: A — concurrent.futures

  20. What is the output of below code?
    1 python
    2 from concurrent.futures import ThreadPoolExecutor
    3 with ThreadPoolExecutor(max_workers=2) as ex:
    4 f = ex.submit(lambda: "Eduinq")
    5 print(f.result())
    a) Eduinq
    b) Error
    c) None
    d) Thread
    View Answer

    Correct answer: A — Eduinq

  21. Which of the following is used to create a thread subclass?
    a) threading.Thread
    b) multiprocessing.Process
    c) concurrent.Future
    d) os.Thread
    View Answer

    Correct answer: A — threading.Thread

  22. What will be printed?
    1 python
    2 import threading
    3 class MyThread(threading.Thread):
    4 def run(self):
    5 print("Eduinq Thread")
    6 t = MyThread()
    7 t.start()
    a) Eduinq Thread
    b) Error
    c) None
    d) Thread
    View Answer

    Correct answer: A — Eduinq Thread

  23. Which of the following is used to create a process subclass?
    a) multiprocessing.Process
    b) threading.Thread
    c) concurrent.Future
    d) os.Process
    View Answer

    Correct answer: A — multiprocessing.Process

  24. What is the output of below code?
    1 python
    2 import multiprocessing
    3 class MyProcess(multiprocessing.Process):
    4 def run(self):
    5 print("Eduinq Process")
    6 p = MyProcess()
    7 p.start()
    8 p.join()
    a) Eduinq Process
    b) Error
    c) None
    d) Process
    View Answer

    Correct answer: A — Eduinq Process

  25. Which of the following is used for thread synchronization?
    a) threading.Semaphore
    b) threading.Lock
    c) threading.Event
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  26. What will be printed?
    1 python
    2 import threading
    3 event = threading.Event()
    4 event.set()
    5 print(event.is_set())
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  27. Which of the following is used for process synchronization?
    a) multiprocessing.Semaphore
    b) multiprocessing.Lock
    c) multiprocessing.Event
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What is the output of below code?
    1 python
    2 import multiprocessing
    3 lock = multiprocessing.Lock()
    4 print(isinstance(lock,multiprocessing.Lock))
    a) False
    b) True
    c) Error
    d) None
    View Answer

    Correct answer: A — False

  29. Which of the following is used for thread communication?
    a) Queue.Queue
    b) multiprocessing.Queue
    c) os.Queue
    d) None
    View Answer

    Correct answer: A — Queue.Queue

  30. What will be printed?
    1 python
    2 import queue
    3 q = queue.Queue()
    4 q.put("Eduinq")
    5 print(q.get())
    a) Eduinq
    b) Error
    c) None
    d) Queue
    View Answer

    Correct answer: A — Eduinq

  31. Which of the following is used for process communication?
    a) multiprocessing.Queue
    b) queue.Queue
    c) os.Queue
    d) None
    View Answer

    Correct answer: A — multiprocessing.Queue

  32. What is the output of below code?
    1 python
    2 import multiprocessing
    3 q = multiprocessing.Queue()
    4 q.put(5)
    5 print(q.get())
    a) 5
    b) Error
    c) None
    d) Queue
    View Answer

    Correct answer: A — 5

  33. Which of the following is used for thread pool?
    a) concurrent.futures.ThreadPoolExecutor
    b) multiprocessing.Pool
    c) threading.Pool
    d) None
    View Answer

    Correct answer: A — concurrent.futures.ThreadPoolExecutor

  34. What will be printed?
    1 python
    2 from concurrent.futures import ThreadPoolExecutor
    3 with ThreadPoolExecutor(max_workers=2) as ex:
    4 f = ex.submit(lambda: "Eduinq")
    5 print(f.result())
    a) Eduinq
    b) Error
    c) None
    d) Thread
    View Answer

    Correct answer: A — Eduinq

  35. Which of the following is used for process pool?
    a) multiprocessing.Pool
    b) concurrent.futures.ProcessPoolExecutor
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  36. What is the output of below code?
    1 python
    2 from concurrent.futures import ProcessPoolExecutor
    3 with ProcessPoolExecutor(max_workers=2) as ex:
    4 f = ex.submit(lambda: 5*5)
    5 print(f.result())
    a) 25
    b) Error
    c) None
    d) 5
    View Answer

    Correct answer: A — 25

  37. Which of the following is used to get current thread?
    a) threading.current_thread()
    b) threading.active_thread()
    c) threading.this_thread()
    d) None
    View Answer

    Correct answer: A — threading.current_thread()

  38. What will be printed?
    1 python
    2 import threading
    3 print(threading.current_thread().name)
    a) MainThread
    b) Thread
    c) Error
    d) None
    View Answer

    Correct answer: A — MainThread

  39. Which of the following is used to get current process?
    a) multiprocessing.current_process()
    b) multiprocessing.active_process()
    c) multiprocessing.this_process()
    d) None
    View Answer

    Correct answer: A — multiprocessing.current_process()

  40. What is the output of below code?
    1 python
    2 import multiprocessing
    3 print(multiprocessing.current_process().name)
    a) MainProcess
    b) Process
    c) Error
    d) None
    View Answer

    Correct answer: A — MainProcess

  41. Which of the following is used to get active thread count?
    a) threading.active_count()
    b) threading.count()
    c) threading.total()
    d) None
    View Answer

    Correct answer: A — threading.active_count()

  42. What will be printed?
    1 python
    2 import threading
    3 print(threading.active_count()>=1)
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  43. Which of the following is used to get active children processes?
    a) multiprocessing.active_children()
    b) multiprocessing.children()
    c) multiprocessing.active_processes()
    d) None
    View Answer

    Correct answer: A — multiprocessing.active_children()

  44. What is the output of below code?
    1 python
    2 import multiprocessing
    3 print(type(multiprocessing.active_children()))
    a) list
    b) dict
    c) Error
    d) None
    View Answer

    Correct answer: A — list

  45. Which of the following is used to set daemon thread?
    a) t.daemon=True
    b) t.setDaemon(True)
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  46. What will be printed?
    1 python
    2 import threading
    3 t = threading.Thread(target=lambda:None)
    4 t.daemon=True
    5 print(t.isDaemon())
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  47. Which of the following is used to set daemon process?
    a) p.daemon=True
    b) p.setDaemon(True)
    c) Both A and B
    d) None
    View Answer

    Correct answer: A — p.daemon=True

  48. What is the output of below code?
    1 python
    2 import multiprocessing
    3 p = multiprocessing.Process(target=lambda:None)
    4 p.daemon=True
    5 print(p.daemon)
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  49. Which of the following is used to terminate a process?
    a) terminate()
    b) stop()
    c) kill()
    d) end()
    View Answer

    Correct answer: A — terminate()

  50. What will be printed?
    1 python
    2 import multiprocessing
    3 p = multiprocessing.Process(target=lambda:None)
    4 p.start()
    5 p.terminate()
    6 print(p.exitcode is not None)
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

Quick Links to Explore