PYTHON AsyncIO and Concurrency MCQs

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

    Correct answer: A — asyncio

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

    Correct answer: A — True

  3. Which keyword is used to define an asynchronous function?
    a) async
    b) await
    c) defasync
    d) None
    View Answer

    Correct answer: A — async

  4. What is the output of below code?
    1 python
    2 import asyncio
    3 async def show():
    4 return "Eduinq"
    5 print(asyncio.run(show()))
    a) Eduinq
    b) Error
    c) None
    d) async
    View Answer

    Correct answer: A — Eduinq

  5. Which keyword is used to wait for coroutine result?
    a) await
    b) async
    c) yield
    d) None
    View Answer

    Correct answer: A — await

  6. What will be printed?
    1 python
    2 import asyncio
    3 async def show():
    4 await asyncio.sleep(1)
    5 return "Eduinq"
    6 print(asyncio.run(show()))
    a) Eduinq
    b) Error
    c) None
    d) sleep
    View Answer

    Correct answer: A — Eduinq

  7. Which of the following is used to gather multiple coroutines?
    a) asyncio.gather()
    b) asyncio.run()
    c) asyncio.wait()
    d) None
    View Answer

    Correct answer: A — asyncio.gather()

  8. What is the output of below code?
    1 python
    2 import asyncio
    3 async def one(): return "E"
    4 async def two(): return "duinq"
    5 print(asyncio.run(asyncio.gather(one(),two())))
    a) ['E','duinq']
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — ['E','duinq']

  9. Which of the following is used to create tasks?
    a) asyncio.create_task()
    b) asyncio.run()
    c) asyncio.gather()
    d) None
    View Answer

    Correct answer: A — asyncio.create_task()

  10. What will be printed?
    1 python
    2 import asyncio
    3 async def show(): return "Eduinq"
    4 async def main():
    5 t = asyncio.create_task(show())
    6 print(await t)
    7 asyncio.run(main())
    a) Eduinq
    b) Error
    c) None
    d) Task
    View Answer

    Correct answer: A — Eduinq

  11. Which of the following is used for event loop?
    a) asyncio.get_event_loop()
    b) asyncio.run()
    c) asyncio.loop()
    d) None
    View Answer

    Correct answer: A — asyncio.get_event_loop()

  12. What is the output of below code?
    1 python
    2 import asyncio
    3 loop = asyncio.get_event_loop()
    4 print(isinstance(loop,asyncio.AbstractEventLoop))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  13. Which of the following is used for futures?
    a) asyncio.Future
    b) concurrent.Future
    c) threading.Future
    d) None
    View Answer

    Correct answer: A — asyncio.Future

  14. What will be printed?
    1 python
    2 import asyncio
    3 f = asyncio.Future()
    4 f.set_result("Eduinq")
    5 print(f.result())
    a) Eduinq
    b) Error
    c) None
    d) Future
    View Answer

    Correct answer: A — Eduinq

  15. Which of the following is used for queues in asyncio?
    a) asyncio.Queue
    b) queue.Queue
    c) multiprocessing.Queue
    d) None
    View Answer

    Correct answer: A — asyncio.Queue

  16. What is the output of below code?
    1 python
    2 import asyncio
    3 async def main():
    4 q = asyncio.Queue()
    5 await q.put("Eduinq")
    6 print(await q.get())
    7 asyncio.run(main())
    a) Eduinq
    b) Error
    c) None
    d) Queue
    View Answer

    Correct answer: A — Eduinq

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

    Correct answer: D — All of the above

  18. What will be printed?
    1 python
    2 import asyncio
    3 async def main():
    4 e = asyncio.Event()
    5 e.set()
    6 print(e.is_set())
    7 asyncio.run(main())
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  19. Which of the following is used for async streams?
    a) async for
    b) async with
    c) await for
    d) None
    View Answer

    Correct answer: A — async for

  20. What is the output of below code?
    1 python
    2 import asyncio
    3 async def main():
    4 async for i in async_iter():
    5 print(i)
    6 (Assume async_iter yields 1,2,3)
    a) 1 2 3
    b) Error
    c) None
    d) async
    View Answer

    Correct answer: A — 1 2 3

  21. Which of the following is used for async context managers?
    a) async with
    b) async for
    c) await with
    d) None
    View Answer

    Correct answer: A — async with

  22. What will be printed?
    1 python
    2 import asyncio
    3 class Eduinq:
    4 async def __aenter__(self): return "Enter"
    5 async def __aexit__(self,exc_type,exc,trace): return "Exit"
    6 async def main():
    7 async with Eduinq() as e:
    8 print(e)
    9 asyncio.run(main())
    a) Enter
    b) Exit
    c) Error
    d) None
    View Answer

    Correct answer: A — Enter

  23. Which of the following is used for async iteration?
    a) aiter
    b) iter
    c) next
    d) None
    View Answer

    Correct answer: A — aiter

  24. What is the output of below code?
    1 python
    2 class Eduinq:
    3 def __aiter__(self): return self
    4 async def __anext__(self): return "E"
    5 (Iterating once)
    a) E
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — E

  25. Which of the following is used for running tasks concurrently?
    a) asyncio.wait()
    b) asyncio.gather()
    c) asyncio.run()
    d) None
    View Answer

    Correct answer: A — asyncio.wait()

  26. What will be printed?
    1 python
    2 import asyncio
    3 async def one(): return "E"
    4 async def two(): return "duinq"
    5 async def main():
    6 done,pending = await asyncio.wait([one(),two()])
    7 for d in done: print(d.result())
    8 asyncio.run(main())
    a) E duinq
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — E duinq

  27. Which of the following is used for running coroutines until complete?
    a) asyncio.run_until_complete()
    b) asyncio.run()
    c) loop.run_until_complete()
    d) None
    View Answer

    Correct answer: C — loop.run_until_complete()

  28. What is the output of below code?
    1 python
    2 import asyncio
    3 loop = asyncio.get_event_loop()
    4 print(loop.run_until_complete(asyncio.sleep(0))==None)
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  29. Which of the following is used for running forever?
    a) loop.run_forever()
    b) asyncio.run()
    c) asyncio.forever()
    d) None
    View Answer

    Correct answer: A — loop.run_forever()

  30. What will be printed?
    1 python
    2 import asyncio
    3 loop = asyncio.get_event_loop()
    4 loop.call_soon(lambda:print("Eduinq"))
    5 loop.run_until_complete(asyncio.sleep(0))
    a) Eduinq
    b) Error
    c) None
    d) Forever
    View Answer

    Correct answer: A — Eduinq

  31. Which of the following is used for scheduling callbacks?
    a) loop.call_soon()
    b) loop.call_later()
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  32. What is the output of below code?
    1 python
    2 import asyncio
    3 loop = asyncio.get_event_loop()
    4 loop.call_later(1,lambda:print("Eduinq"))
    5 loop.run_until_complete(asyncio.sleep(1.1))
    a) Eduinq
    b) Error
    c) None
    d) Later
    View Answer

    Correct answer: A — Eduinq

  33. Which of the following is used for async subprocesses?
    a) asyncio.create_subprocess_exec()
    b) subprocess.run()
    c) os.system()
    d) None
    View Answer

    Correct answer: A — asyncio.create_subprocess_exec()

  34. What will be printed?
    1 python
    2 import asyncio
    3 async def main():
    4 proc = await asyncio.create_subprocess_exec("echo","Eduinq")
    5 await proc.wait()
    6 asyncio.run(main())
    a) Eduinq
    b) Error
    c) None
    d) echo
    View Answer

    Correct answer: A — Eduinq

  35. Which of the following is used for async streams?
    a) StreamReader, StreamWriter
    b) FileReader, FileWriter
    c) InputStream, OutputStream
    d) None
    View Answer

    Correct answer: A — StreamReader, StreamWriter

  36. What is the output of below code?
    1 python
    2 import asyncio
    3 async def main():
    4 r,w = await asyncio.open_connection("example.com",80)
    5 print(isinstance(r,asyncio.StreamReader))
    6 asyncio.run(main())
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  37. Which of the following is used for async tasks cancellation?
    a) task.cancel()
    b) task.stop()
    c) task.end()
    d) None
    View Answer

    Correct answer: A — task.cancel()

  38. What will be printed?
    1 python
    2 import asyncio
    3 async def show():
    4 await asyncio.sleep(5)
    5 return "Eduinq"
    6 async def main():
    7 t = asyncio.create_task(show())
    8 t.cancel()
    9 try: await t
    10 except asyncio.CancelledError: print("Cancelled")
    11 asyncio.run(main())
    a) Cancelled
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Cancelled

  39. Which of the following is used for async timeouts?
    a) asyncio.wait_for()
    b) asyncio.timeout()
    c) asyncio.run()
    d) None
    View Answer

    Correct answer: A — asyncio.wait_for()

  40. What is the output of below code?
    1 python
    2 import asyncio
    3 async def show():
    4 await asyncio.sleep(2)
    5 return "Eduinq"
    6 async def main():
    7 try:
    8 print(await asyncio.wait_for(show(),timeout=1))
    9 except asyncio.TimeoutError: print("Timeout")
    10 asyncio.run(main())
    a) Timeout
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Timeout

  41. Which of the following is used for async shield?
    a) asyncio.shield()
    b) asyncio.protect()
    c) asyncio.safe()
    d) None
    View Answer

    Correct answer: A — asyncio.shield()

  42. What will be printed?
    1 python
    2 import asyncio
    3 async def show():
    4 await asyncio.sleep(1)
    5 return "Eduinq"
    6 async def main():
    7 t = asyncio.create_task(show())
    8 print(await asyncio.shield(t))
    9 asyncio.run(main())
    a) Eduinq
    b) Error
    c) None
    d) Shield
    View Answer

    Correct answer: A — Eduinq

  43. Which of the following is used for async condition variables?
    a) asyncio.Condition
    b) asyncio.Lock
    c) asyncio.Semaphore
    d) None
    View Answer

    Correct answer: A — asyncio.Condition

  44. What is the output of below code?
    1 python
    2 import asyncio
    3 async def main():
    4 cond = asyncio.Condition()
    5 async with cond:
    6 print(isinstance(cond,asyncio.Condition))
    7 asyncio.run(main())
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  45. Which of the following is used for async barriers?
    a) asyncio.Barrier
    b) threading.Barrier
    c) multiprocessing.Barrier
    d) None
    View Answer

    Correct answer: B — threading.Barrier

  46. What will be printed?
    1 python
    2 import threading
    3 b = threading.Barrier(2)
    4 print(isinstance(b,threading.Barrier))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  47. Which of the following is used for async tasks grouping?
    a) asyncio.TaskGroup
    b) asyncio.Group
    c) asyncio.Tasks
    d) None
    View Answer

    Correct answer: A — asyncio.TaskGroup

  48. What is the output of below code?
    1 python
    2 import asyncio
    3 async def main():
    4 async with asyncio.TaskGroup() as tg:
    5 tg.create_task(asyncio.sleep(0))
    6 print("Done")
    7 asyncio.run(main())
    a) Done
    b) Error
    c) None
    d) Task
    View Answer

    Correct answer: A — Done

  49. Which of the following is used for async run in threads?
    a) asyncio.to_thread()
    b) asyncio.run_in_executor()
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  50. What will be printed?
    1 python
    2 import asyncio
    3 def show(): return "Eduinq"
    4 async def main():
    5 print(await asyncio.to_thread(show))
    6 asyncio.run(main())
    a) Eduinq
    b) Error
    c) None
    d) Thread
    View Answer

    Correct answer: A — Eduinq

Quick Links to Explore