All quizzesEasy
Coroutines & asyncio.run — Series 2
Preview — 3 of 10 questions
What is the output of the following code?
javascript
import asyncio
async def count_up(n):
for i in range(n):
await asyncio.sleep(0)
yield i
async def main():
result = []
async for x in count_up(3):
result.append(x)
print(result)
asyncio.run(main())A[] — an async def function containing yield cannot be iterated with async for.
B<async_generator object count_up at 0x...>
CTypeError: 'async_generator' object is not iterable
D[0, 1, 2]
What is the output of the following code?
javascript
import asyncio
async def compute():
return 42
async def main():
coro = compute()
print(await coro)
print(await coro)
asyncio.run(main())A42, then RuntimeError: cannot reuse already awaited coroutine
B42, then 42 — coroutine objects can be awaited repeatedly, each time re-running the body from scratch.
C42, then None — awaiting an already-completed coroutine a second time silently returns None.
DTypeError: coroutine.send() called on an exhausted coroutine
What is the output of the following code?
javascript
import asyncio
async def compute():
return 1
coro = compute()
print(asyncio.iscoroutine(coro))
print(asyncio.iscoroutine(compute))AFalse, True — iscoroutine checks the function itself, not the object it produces when called.
BTrue, False
CTrue, True
DFalse, False
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.