Python Quizz Dive into our tech quiz zone and put your technical skills to the test! Our quizzes cover a wide array of technical topics, perfect for sharpening your knowledge and challenging your understanding. Compete with others, see your rankings, and boost your technical proficiency. Start quizzing today! 1 / 70 1. Which of the following is a Python memory management technique? Memory pooling All of the above Garbage collection Reference counting 2 / 70 2. Which module is used for debugging in Python? pdb traceback debug logging 3 / 70 3. What is the output of the following code? a = [1, 2, 3] b = a a = a + [4, 5] print(b) [1, 2, 3, 4, 5] [1, 2, 3] TypeError None 4 / 70 4. What is the purpose of the tell method in file handling? To write to a file To close a file To read a file To get the current file pointer position 5 / 70 5. What will be the output of the following code? import multiprocessing def print_numbers(): for i in range(5): print(i) p1 = multiprocessing.Process(target=print_numbers) p2 = multiprocessing.Process(target=print_numbers) p1.start() p2.start() p1.join() p2.join() 5 5 5 5 5 0 0 1 1 2 2 3 3 4 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 6 / 70 6. Which of the following is true about Python inheritance? A class can inherit from itself A class can inherit from one class only A class can inherit from multiple classes A class cannot inherit from another class 7 / 70 7. What will be the output of the following code? class A: pass obj = A() print(type(obj).__name__) A class object None 8 / 70 8. What will be the output of the following code? import heapq h = [3, 1, 4, 1, 5, 9, 2, 6] heapq.heapify(h) print([heapq.heappop(h) for _ in range(3)]) [1, 3, 4] [3, 1, 4] [1, 4, 1] [1, 1, 2] 9 / 70 9. What is the purpose of the GIL (Global Interpreter Lock) in Python? To allow multiple threads to execute Python bytecodes at once To handle exceptions To manage memory allocation To prevent multiple threads from executing Python bytecodes at once 10 / 70 10. What does the __getattr__ method do in a Python class? It is called when an attribute is accessed It is called when an attribute is set It is called when an attribute does not exist It is called when an attribute is deleted 11 / 70 11. What will be the output of the following code? import weakref class A: pass a = A() r = weakref.ref(a) print(r()) del a print(r()) TypeError None and None A object at ... and A object at ... A object at ... and None 12 / 70 12. What will be the output of the following code? from itertools import cycle colors = ['red', 'green', 'blue'] cycle_colors = cycle(colors) for _ in range(5): print(next(cycle_colors)) red green blue red green red green blue blue blue None red green blue 13 / 70 13. Which of the following is true about context managers in Python? All of the above They use the with statement They must define __enter__ and __exit__ methods They are used to allocate and release resources 14 / 70 14. Which of the following is true about Python's garbage collector? It can be manually controlled It uses reference counting It uses generational garbage collection All of the above 15 / 70 15. What does the yield from statement do in Python? It is used to throw an exception in a generator It is used to delegate part of a generator's operations to another generator It is used to exit a generator It is used to return multiple values 16 / 70 16. What is the purpose of the __del__ method in a Python class? To create an instance of the class To initialize an instance of the class To modify an instance of the class To delete an instance of the class 17 / 70 17. What is the purpose of the __new__ method in a Python class? To modify an instance of the class To delete an instance of the class To initialize a new instance of the class To create a new instance of the class 18 / 70 18. What is the purpose of the partial function in the functools module? To modify the return value of a function To reduce the number of arguments to a function To apply a function partially To create a partial function 19 / 70 19. Which method in the re module is used to search for a pattern in a string? match() search() find() lookup() 20 / 70 20. Which method is used to read a specific number of bytes from a file? readline() read() readlines() read(size) 21 / 70 21. What will be the output of the following code? import sys a = [] b = a print(sys.getrefcount(a)) 2 3 4 1 22 / 70 22. What will be the output of the following code? def func(a, b=[]): b.append(a) return b print(func(1)) print(func(2)) [1] and [2, 2] [1] and [2] [1] and [1, 2] [1, 1] and [2, 2] 23 / 70 23. What is the purpose of the __call__ method in a Python class? To initialize an object To delete an object To compare objects To make an object callable like a function 24 / 70 24. Which method is used to replace all occurrences of a pattern in a string? re.findall() re.search() re.sub() re.replace() 25 / 70 25. What is the result of the following code? class A: def __init__(self): self.a = 1 self.__b = 2 class B(A): def __init__(self): super().__init__() self.a = 3 self.__b = 4 obj = B() print(obj.a) print(obj._A__b) 3 and AttributeError 3 and 4 3 and 2 1 and 2 26 / 70 26. Which function is used to get the reference count of an object in Python? ref.get_count() gc.get_refcount() memory.get_refcount() sys.getrefcount() 27 / 70 27. Which module is used for JSON manipulation in Python? ujson json simplejson xml 28 / 70 28. What will be the output of the following code? import logging logging.basicConfig(level=logging.INFO) logging.debug('Debug message') logging.info('Info message') logging.warning('Warning message') Warning message None Info messagenWarning message Debug messagenInfo messagenWarning message 29 / 70 29. What is the output of the following code? from collections import deque d = deque([1, 2, 3, 4]) d.appendleft(0) d.pop() d.extend([5, 6]) d.rotate(1) print(d) deque([5, 6, 0, 1, 2, 3]) deque([6, 0, 1, 2, 3, 5]) deque([0, 1, 2, 3, 5, 6]) deque([6, 0, 1, 2, 3, 4]) 30 / 70 30. What will be the output of the following code? def decorator_func(func): def wrapper(*args, **kwargs): print("Before") result = func(*args, **kwargs) print("After") return result return wrapper @decorator_func def say_hello(): print("Hello!") say_hello() BeforenAfternHello! Hello!nAfternBefore BeforenHello!nAfter Hello!nBeforenAfter 31 / 70 31. Which module is used for asynchronous programming in Python? multiprocessing concurrent.futures threading asyncio 32 / 70 32. Which module is used for creating processes in Python? multiprocessing asyncio concurrent.futures threading 33 / 70 33. What will be the output of the following code? import os os.chdir('/tmp') print(os.getcwd()) tmp /tmp / None 34 / 70 34. Which module is used to create and manage complex data structures in Python? itertools collections functools heapq 35 / 70 35. Which method is used to dynamically create a class in Python? object() meta() class() type() 36 / 70 36. 56. What will be the output of the following code? class A: def __init__(self, x): self.x = x def __str__(self): return f'Value: {self.x}' obj = A(10) print(obj) A object at ... Value: 10 None 10 37 / 70 37. What will be the output of the following code? def generator_func(): yield 1 yield 2 yield 3 g = generator_func() print(next(g)) print(next(g)) print(next(g)) print(next(g)) 1 2 3 Error 1 2 3 None 1 2 3 Exception 1 2 3 StopIteration 38 / 70 38. What is the purpose of the @functools.wraps decorator in Python? To preserve the metadata of the original function To create a new function To time the execution of a function To modify the return value of a function 39 / 70 39. What will be the output of the following code? from collections import defaultdict d = defaultdict(int) d['a'] += 1 d['b'] += 2 d['c'] += 3 print(d['a'], d['b'], d['c'], d['d']) 1 2 3 0 1 2 3 KeyError None 0 0 0 0 40 / 70 40. What will be the output of the following code? import re pattern = re.compile(r'(\d{3})-(\d{2})-(\d{4})') match = pattern.match('123-45-6789') print(match.groups()) ('123-45', '6789') ('123', '456', '789') ('123', '45', '6789') ('123-45-6789') 41 / 70 41. What is the purpose of the await keyword in Python? To terminate a coroutine To pause the execution of a coroutine To create a coroutine To suspend the execution of a coroutine until the awaited result is available 42 / 70 42. What will be the output of the following code? from functools import reduce result = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]) print(result) 15 1 10 5 43 / 70 43. What is the purpose of the seek method in file handling? To move the file pointer to a specific position To close a file To read a file To write to a file 44 / 70 44. What is the purpose of the gc module in Python? To provide access to the garbage collector To provide access to file handling functions To provide access to memory management functions To provide access to reference counting functions 45 / 70 45. What will be the output of the following code? def func(x, y, z=3, *args, **kwargs): return x + y + z + sum(args) + sum(kwargs.values()) print(func(1, 2, 3, 4, 5, a=6, b=7)) 29 None 15 28 46 / 70 46. What will be the output of the following code? import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') if __name__ == '__main__': unittest.main() Test fails Test passes ImportError Syntax error 47 / 70 47. What is the purpose of the set_trace method in the pdb module? To set a breakpoint To start a debugging session To display the traceback To step through the code 48 / 70 48. Which module is used for mocking in Python? unittest testing unittest.mock mocking 49 / 70 49. What is the purpose of the @patch decorator in the unittest.mock module? To create a test suite To raise an exception To replace a method or an object with a mock To run the tests 50 / 70 50. Which method is used to convert a string to a frozenset? to_frozenset() set() convert() frozenset() 51 / 70 51. Which of the following is true about coroutines in Python? They use the yield keyword They are a special kind of generator function They use the async def keyword Both A and C 52 / 70 52. What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello, World!') with open('test.txt', 'rb') as f: print(f.read()) None Hello, World! FileNotFoundError b'Hello, World!' 53 / 70 53. What does the following code do? class C: def __init__(self, x): self.x = x def __call__(self, y): return self.x + y obj = C(10) print(obj(5)) 15 None 10 TypeError 54 / 70 54. What will be the output of the following code? def decorator_func(func): def wrapper(*args, **kwargs): print("Wrapper executed this before {}".format(func.__name__)) return func(*args, **kwargs) return wrapper @decorator_func def display(): print("Display function ran") display() Display function ran Wrapper executed this before display Wrapper executed this before displaynDisplay function ran Display function rannWrapper executed this before display 55 / 70 55. Which of the following is true about namedtuples? Both B and C They are mutable They are ordered They can be indexed by field names and positions 56 / 70 56. What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello, World!') with open('test.txt', 'r') as f: print(f.read()) FileNotFoundError `` None Hello, World! 57 / 70 57. What will be the output of the following code? import pdb def test(): pdb.set_trace() print("Hello, World!") test() None Hello, World! Hello, World!npdb> pdb> 58 / 70 58. Which module is used to handle date and time in Python? time datetime date calendar 59 / 70 59. What does the following code do? from contextlib import contextmanager @contextmanager def open_file(name): f = open(name, 'w') try: yield f finally: f.close() with open_file('test.txt') as f: f.write('Hello, World!') Raises a FileNotFoundError Does nothing Writes Hello, World! to test.txt Raises a SyntaxError 60 / 70 60. What will be the output of the following code? import threading def print_numbers(): for i in range(5): print(i) t1 = threading.Thread(target=print_numbers) t2 = threading.Thread(target=print_numbers) t1.start() t2.start() t1.join() t2.join() 0 0 1 1 2 2 3 3 4 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 5 5 5 5 5 61 / 70 61. What will be the output of the following code? import re pattern = re.compile(r'\d+') result = pattern.sub('#', 'My number is 123 and my friend's number is 456') print(result) None My number is 123 and my friend's number is # My number is 123 and my friend's number is 456 My number is # and my friend's number is # 62 / 70 62. What will be the output of the following code? class A: def __init__(self, x): self.x = x def __add__(self, other): return A(self.x + other.x) obj1 = A(1) obj2 = A(2) obj3 = obj1 + obj2 print(obj3.x) 3 2 TypeError 1 63 / 70 63. What is the output of the following code? def func(x): return lambda y: x + y f = func(10) print(f(5)) 5 15 TypeError 10 64 / 70 64. Which method in the os module is used to change the current working directory? os.getcwd() os.mkdir() os.chdir() os.rmdir() 65 / 70 65. What is the purpose of the nonlocal keyword in Python? To delete a variable To declare a global variable To modify a variable in an enclosing scope To indicate that a variable is not local 66 / 70 66. Which of the following is true about lambda functions in Python? They can have multiple statements They are anonymous functions Both B and C They are defined using the def keyword 67 / 70 67. What will be the output of the following code? import re pattern = re.compile(r'\d+') result = pattern.findall('My number is 123 and my friend's number is 456') print(result) ['My number is', 'and my friend's number is'] ['123', '456'] ['123456'] ['123'] 68 / 70 68. What is the purpose of the __slots__ attribute in a Python class? To limit the memory footprint of instances To delete an instance of the class To create a new instance of the class To initialize a new instance of the class 69 / 70 69. Which method is used to read all lines of a file into a list? readall() read() readline() readlines() 70 / 70 70. What will be the output of the following code? class A: def __init__(self): self.value = 42 obj = A() print(getattr(obj, 'value')) 42 0 None AttributeError Your score is 0%