What does the following async code output? import asyncio async def task(n): await asyncio.sleep(0) return n * 2 async def main(): results = await asyncio.gather(task(1), task(2), task(3)) print(results) asyncio.run(main()) Python ProfessionalHard Try Now
What is the output of the following? import weakref class Obj: pass obj = Obj() ref = weakref.ref(obj) print(ref() is obj) del obj print(ref()) Python ProfessionalHard Try Now
What does the following code demonstrate about Python’s name mangling? class Secure: def __init__(self): self.__secret = 42 s = Secure() print(s.__secret) Python ProfessionalHard Try Now
What is the output of the following code using __slots__ with inheritance? Python ProfessionalHard Try Now
What is the difference between @staticmethod and a module-level function, and when should you prefer one over the other? Python ProfessionalHard Try Now
What does the following code output? from dataclasses import dataclass, field @dataclass class Config: tags: list = field(default_factory=list) c1 = Config() c2 = Config() c1.tags.append(‘x’) print(c2.tags) Python ProfessionalHard Try Now
What is ExceptionGroup in Python 3.11+ and how does except* differ from except? Python ProfessionalMedium Try Now
What does the following code output? def make_counter(): count = 0 def increment(n=1): nonlocal count count += n return count def reset(): nonlocal count count = 0 increment.reset = reset return increment c = make_counter() c(3) c(2) c.reset() print(c()) Python ProfessionalHard Try Now
What is the output of the following code? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v) Python ProfessionalHard Try Now
What does the following code output? from contextlib import contextmanager @contextmanager def managed(): print(‘enter’) try: yield 42 finally: print(‘exit’) with managed() as v: print(v) Python ProfessionalHard Try Now
What is the correct way to implement a thread-safe singleton in Python using a lock? Python ProfessionalHard Try Now
What is the output of the following code? class Singleton: _instance = None def __new__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance a = Singleton() b = Singleton() print(a is b, id(a) == id(b)) Python ProfessionalHard Try Now
What does the following code output? from functools import wraps def debug(func): @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @debug def greet(name): ”’Says hello”’ return f’Hello {name}’ print(greet.__name__, greet.__doc__) Python ProfessionalHard Try Now
What does the following code output? def outer(): x = 10 def inner(): nonlocal x x += 1 return x return inner f = outer() print(f(), f(), f()) Python ProfessionalHard Try Now
In Python’s data model, when is __new__ called versus __init__, and what must __new__ return? Python ProfessionalHard Try Now