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
In Python’s data model, when is __new__ called versus __init__, and what must __new__ return? 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
What is the purpose of __init_subclass__ compared to a metaclass, and what are its limitations? Python ProfessionalHard Try Now
What is the output of the following code? from typing import Protocol class Drawable(Protocol): def draw(self) -> None: … class Circle: def draw(self) -> None: print(‘circle’) def render(shape: Drawable) -> None: shape.draw() render(Circle()) Python ProfessionalHard Try Now
What does the following code demonstrate about Python’s GC and reference cycles? import gc class Node: def __init__(self): self.ref = None a = Node() b = Node() a.ref = b b.ref = a del a, b print(gc.collect()) Python ProfessionalHard Try Now
What is the purpose of __class_getitem__ in Python and how does it enable generic type hints? Python ProfessionalHard Try Now
What does the following code output? import sys class MyList(list): def __sizeof__(self): return super().__sizeof__() + 100 ml = MyList([1, 2, 3]) print(sys.getsizeof(ml) – sys.getsizeof([1, 2, 3])) Python ProfessionalHard Try Now
What is the difference between asyncio.gather() and asyncio.TaskGroup (Python 3.11+)? Python ProfessionalHard Try Now
Which of the following correctly explains how Python’s asyncio event loop executes coroutines? Python ProfessionalHard Try Now
What is the output of the following code involving __missing__? class DefaultList(dict): def __missing__(self, key): self[key] = [] return self[key] d = DefaultList() d[‘a’].append(1) d[‘a’].append(2) d[‘b’].append(3) print(d) Python ProfessionalHard Try Now
What does the following descriptor code output? class Validator: def __set_name__(self, owner, name): self.name = name def __get__(self, obj, objtype=None): if obj is None: return self return obj.__dict__.get(self.name) def __set__(self, obj, value): if not isinstance(value, int): raise TypeError(f'{self.name} must be int’) obj.__dict__[self.name] = value class Point: x = Validator() p = Point() p.x = 5 print(p.x) Python ProfessionalHard Try Now
What is the output of the following code? class Meta(type): def __new__(mcs, name, bases, namespace): namespace[‘class_id’] = name.upper() return super().__new__(mcs, name, bases, namespace) class Foo(metaclass=Meta): pass print(Foo.class_id) Python ProfessionalHard Try Now
What does the following code output? from collections import namedtuple Point = namedtuple(‘Point’, [‘x’, ‘y’]) p = Point(3, 4) print(p.x, p[1], len(p)) Python DeveloperMedium Try Now
What does the following code output? def make_adder(n): return lambda x: x + n add5 = make_adder(5) print(add5(3), add5(10)) Python DeveloperMedium Try Now
What is the difference between append() and extend() on a Python list? Python DeveloperMedium Try Now