Quick check
1. What is the output of the following code? def recursive_sum(n): if n <= 1: return n else: return n + recursive_sum(n - 1) print(recursive_sum(5))
2. What will be the output of the following code? class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def speak(self): print("Dog barks") class Labrador(Dog): pass lab = Labrador() lab.speak()
3. What is the output of the following code? def modify_list(lst): lst.append(4) lst = [1, 2, 3] print("Inside function:", lst) my_list = [1, 2, 3] modify_list(my_list) print("Outside function:", my_list)
4. What is the purpose of the `__init__` method in Python classes?
5. What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)
6. Which of the following is true about Python sets?
7. What is the output of the following code? def outer(): x = 1 def inner(): nonlocal x x = 2 print("Inner:", x) inner() print("Outer:", x) outer()
8. What is the purpose of the `super()` function in Python?
9. What is the output of the following code? class A: def __init__(self): self.x = 1 class B(A): def __init__(self): super().__init__() self.y = 2 b = B() print(b.x, b.y)
10. What is the time complexity of accessing an element in a dictionary by its key?
11. What is the output of the following code? def generator(): yield 1 yield 2 yield 3 g = generator() print(next(g)) print(next(g))
12. Which of the following is NOT a valid way to create a set in Python?
13. What is the output of the following code? class MyClass: class_var = 0 def __init__(self): self.instance_var = 0 def increment(self): self.instance_var += 1 self.class_var += 1 obj1 = MyClass() obj2 = MyClass() obj1.increment() obj2.increment() print(obj1.instance_var, obj1.class_var) print(obj2.instance_var, obj2.class_var)
14. What is the purpose of the `__str__` method in Python classes?
15. What is the output of the following code? def decorator(func): def wrapper(): print("Before") func() print("After") return wrapper @decorator def say_hello(): print("Hello") say_hello()
16. Which of the following is true about Python's garbage collection?
17. What is the output of the following code? import copy original = [[1, 2, 3], [4, 5, 6]] shallow = copy.copy(original) deep = copy.deepcopy(original) original[0][1] = 10 print(shallow[0][1], deep[0][1])
18. What is the purpose of the `__slots__` attribute in Python classes?
19. What is the output of the following code? class Meta(type): def __new__(cls, name, bases, attrs): attrs['x'] = 10 return super().__new__(cls, name, bases, attrs) class MyClass(metaclass=Meta): pass obj = MyClass() print(obj.x)
20. Which of the following is NOT a valid magic method in Python?
21. What is the output of the following code? def func(a, b, c=3, d=4): print(a, b, c, d) func(1, *(5,), **{'c': 9})
22. What is the purpose of the `contextlib` module in Python?
23. What is the output of the following code? class A: def __init__(self): print("A") super().__init__() class B: def __init__(self): print("B") super().__init__() class C(A, B): def __init__(self): print("C") super().__init__() C()
24. Which of the following is true about Python's Global Interpreter Lock (GIL)?
25. What is the output of the following code? import asyncio async def foo(): print("start") await asyncio.sleep(1) print("end") asyncio.run(foo())
26. What is the purpose of the `__all__` variable in Python modules?
27. What is the output of the following code? class MyDict(dict): def __setitem__(self, key, value): super().__setitem(key, value * 2) d = MyDict() d['a'] = 1 print(d['a'])
28. Which of the following is true about Python's memory management?
29. What is the output of the following code? from functools import lru_cache @lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) print(fib(100))
30. What is the purpose of the `__slots__` attribute in Python classes?