Python Quizz

Home Python Quiz

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 method in the re module is used to search for a pattern in a string?

2 / 70

2. What does the __getattr__ method do in a Python class?

3 / 70

3. What will be the output of the following code?

def func(a, b=[]):
b.append(a)
return b

print(func(1))
print(func(2))

4 / 70

4. Which module is used for JSON manipulation in Python?

5 / 70

5. 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))

6 / 70

6. Which method is used to convert a string to a frozenset?

7 / 70

7. 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))

8 / 70

8. What is the purpose of the gc module in Python?

9 / 70

9. 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())

10 / 70

10. What is the purpose of the seek method in file handling?

11 / 70

11. What is the output of the following code?

a = [1, 2, 3]
b = a
a = a + [4, 5]
print(b)

12 / 70

12. Which of the following is true about coroutines in Python?

13 / 70

13. What is the purpose of the __new__ method in a Python class?

14 / 70

14. What will be the output of the following code?
class A:
def __init__(self):
self.value = 42

obj = A()
print(getattr(obj, 'value'))

15 / 70

15. 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!')

16 / 70

16. What is the purpose of the __slots__ attribute in a Python class?

17 / 70

17. 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)

18 / 70

18. Which module is used to create and manage complex data structures in Python?

19 / 70

19. What will be the output of the following code?
import os

os.chdir('/tmp')
print(os.getcwd())

20 / 70

20. 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()

21 / 70

21. Which method is used to read a specific number of bytes from a file?

22 / 70

22. Which module is used for mocking in Python?

23 / 70

23. 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)

24 / 70

24. 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')

25 / 70

25. 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)

26 / 70

26. Which of the following is true about context managers in Python?

27 / 70

27. What is the purpose of the GIL (Global Interpreter Lock) in Python?

28 / 70

28. What is the purpose of the @patch decorator in the unittest.mock module?

29 / 70

29. Which function is used to get the reference count of an object in Python?

30 / 70

30. 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)

31 / 70

31. Which of the following is true about Python's garbage collector?

32 / 70

32. Which module is used to handle date and time in Python?

33 / 70

33. What will be the output of the following code?
class A:
pass

obj = A()
print(type(obj).__name__)

34 / 70

34. Which of the following is true about namedtuples?

35 / 70

35. What is the purpose of the tell method in file handling?

36 / 70

36. What will be the output of the following code?
import sys

a = []
b = a
print(sys.getrefcount(a))

37 / 70

37. 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()

38 / 70

38. 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))

39 / 70

39. 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))

40 / 70

40. What is the purpose of the __del__ method in a Python class?

41 / 70

41. 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())

42 / 70

42. What is the purpose of the set_trace method in the pdb module?

43 / 70

43. 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()

44 / 70

44. 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)

45 / 70

45. 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)

46 / 70

46. What is the purpose of the await keyword in Python?

47 / 70

47. What is the purpose of the nonlocal keyword in Python?

48 / 70

48. 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)])

49 / 70

49. 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'])

50 / 70

50. Which method is used to read all lines of a file into a list?

51 / 70

51. 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())

52 / 70

52. Which method is used to dynamically create a class in Python?

53 / 70

53. Which module is used for creating processes in Python?

54 / 70

54. Which of the following is true about lambda functions in Python?

55 / 70

55. Which module is used for debugging in Python?

56 / 70

56. 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)

57 / 70

57. Which method is used to replace all occurrences of a pattern in a string?

58 / 70

58. What is the purpose of the partial function in the functools module?

59 / 70

59. What is the purpose of the __call__ method in a Python class?

60 / 70

60. Which method in the os module is used to change the current working directory?

61 / 70

61. What is the purpose of the @functools.wraps decorator in Python?

62 / 70

62. What will be the output of the following code?
import pdb

def test():
pdb.set_trace()
print("Hello, World!")

test()

63 / 70

63. 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()

64 / 70

64. What does the yield from statement do in Python?

65 / 70

65. Which of the following is a Python memory management technique?

66 / 70

66. What is the output of the following code?
def func(x):
return lambda y: x + y

f = func(10)
print(f(5))

67 / 70

67. Which module is used for asynchronous programming in Python?

68 / 70

68. Which of the following is true about Python inheritance?

69 / 70

69. 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()

70 / 70

70. 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())

Your score is

0%