asyncio.gather() runs multiple coroutines concurrently and returns a list of their results in the order they were passed. Since task_two takes 1 second and task_one takes 2 seconds, they run concurrently, finishing in about 2 seconds total. A common trap is thinking gather runs them sequentially.
Question 2 · typing · junior
What happens at runtime when the following code is executed?
def identity(x: int) -> int:
return x
result = identity("hello")
A.It raises a SyntaxError
B.It automatically casts "hello" to an integer
C.It executes successfully and returns "hello"
D.It raises a TypeError at runtime
Show answer
C. It executes successfully and returns "hello"
Python type hints are purely advisory for static type checkers (like mypy or IDEs) and documentation; standard Python execution does not enforce them at runtime. Therefore, passing a string to a function annotated for integers executes successfully. A common trap is expecting a TypeError at runtime.
Question 3 · decorators · junior
Which of the following is a limitation of this simple memoize decorator?
def memoize(func):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrapper
A.It only works with keyword arguments
B.It fails if arguments include mutable unhashable types like lists
C.It modifies the global scope permanently
D.It caches functions asynchronously
Show answer
B. It fails if arguments include mutable unhashable types like lists
This is a basic memoization decorator that stores function results in a dictionary cache based on arguments. A common trap is that using *args as a dictionary key fails if any argument is a mutable object (like a list) because lists are unhashable.