Python Quiz (200 Questions)

Python Quiz (Questions 1–50)

Section 1: Introduction to Python & Basic Syntax (1–10)

1. What is Python primarily known as?

2. How do you start the Python interactive interpreter (REPL) from a terminal?

3. In Python, how are code blocks defined?

4. How do you display "Hello World" in Python?

5. Which extension is commonly used for Python files?

6. How do you write a single-line comment in Python?

7. Python 3 print function requires what around the text to print?

8. Python code indentation typically uses how many spaces?

9. Which function retrieves input from the user in Python 3?

10. Which command do you use to run a Python script file from the terminal?

11. Which of the following is NOT a built-in data type in Python?

12. Strings in Python are:

13. Which function gives the data type of a variable in Python?

14. In Python, variables are:

15. Which built-in constant represents the absence of a value?

16. Booleans in Python are represented by which keywords?

17. Which of the following is immutable?

18. To convert a string '123' to an integer:

19. The identity operator 'is' checks if:

20. Which function gives the memory address of an object?

21. Which operator is used for exponentiation in Python?

22. The integer division operator in Python is:

23. The 'in' operator checks:

24. Which operator has higher precedence: * or + ?

25. The modulo operator % does what?

26. x += 5 is equivalent to:

27. The 'not' operator in Python is used for:

28. 'is' checks object identity, while '==' checks:

29. Bitwise AND operator is represented by:

30. Which is true about short-circuit evaluation in Python?

31. Which keyword introduces a conditional block?

32. 'elif' is used in Python to:

33. A 'for' loop in Python typically iterates over:

34. The 'while' loop continues until:

35. 'break' in a loop does what?

36. 'continue' in a loop does what?

37. 'range(5)' generates:

38. The 'else' clause on a loop executes when:

39. 'pass' statement is used for:

40. Which is correct for an if-else block?

41. How do you define a function in Python?

42. If a function doesn't have a return statement, it returns:

43. Default function parameters are specified using:

44. *args in a function definition means:

45. **kwargs in a function definition means:

46. Variables defined inside a function are by default:

47. The 'global' keyword is used to:

48. 'return' keyword in a function does what?

49. Recursive functions are functions that:

50. Lambda functions in Python are:

51. How do you import a module named 'math' in Python?

52. What does `from math import sqrt` do?

53. `__name__ == "__main__"` is used to:

54. Where does Python look for modules by default?

55. A package is defined by the presence of:

56. `pip` is used to:

57. Virtual environments (venv) are used to:

58. `dir(module)` function returns:

59. Using `import module as m` does what?

60. `help(module)` function:

61. Lists in Python are:

62. Tuples in Python are:

63. Which method adds an item to the end of a list?

64. Negative indexing in lists means:

65. list slicing syntax `list[start:end]` returns:

66. `list.sort()` method does what?

67. To quickly create a list of squares, you can use:

68. Tuples can be used for:

69. To unpack a tuple (x, y) into variables a and b:

70. `enumerate(list)` is often used to:

71. A dictionary in Python is:

72. A set in Python is:

73. To get all keys of a dictionary as a list (in Python 3.x you get a view), you use:

74. To add a key-value pair to a dictionary:

75. Which data structure is best for checking membership quickly?

76. dictionary.get(key) method does what if key not found?

77. set operations like union or intersection are available as:

78. Keys in a dictionary must be:

79. `pop` method on a dictionary does what?

80. To remove duplicates from a list, you could convert it to a:

81. How do you get a substring from index 2 to 5 in a string s?

82. Which method returns a string in uppercase?

83. `s.strip()` does what?

84. `s.find('x')` returns:

85. `s.split()` without arguments splits a string by:

86. `join()` method is used to:

87. f-strings are defined using:

88. `str.upper()` returns:

89. `in` operator on a string checks for:

90. `str.replace(old, new)` returns:

91. How do you open a file in Python for reading?

92. The `with` statement is used with files to:

93. file.read() without arguments reads:

94. file.readline() reads:

95. file.write("Hello") does what?

96. 'r+' mode in open means:

97. To ensure safe file handling use:

98. To read all lines of a file into a list:

99. `with open("data.txt", "w") as f:` does what if data.txt exists?

100. Binary mode is indicated by:

101. How do you define a class in Python?

102. The __init__ method in a class:

103. 'self' parameter in methods refers to:

104. To create an instance of a class MyClass:

105. Class attributes are:

106. instance attributes are usually set in:

107. Inheritance in Python is defined by:

108. `isinstance(obj, Class)` returns True if:

109. Encapsulation in Python is done by:

110. OOP stands for:

111. Multiple inheritance means:

112. The `super()` function is used to:

113. `__str__` and `__repr__` methods are special methods used for:

114. @property decorator is used to:

115. Static methods in Python are defined using:

116. An abstract base class (ABC) requires importing:

117. An abstract method must be:

118. Polymorphism refers to:

119. Operator overloading is done by:

120. Design patterns in OOP are:

121. try/except block is used for:

122. If no exception occurs in a try block:

123. `raise` keyword does what?

124. finally block executes:

125. Custom exceptions are created by:

126. assert statement is used to:

127. except Exception as e:

128. Avoiding bare except (i.e. except:) is recommended because:

129. Else clause in try-except-else runs if:

130. try-except-finally structure ensures:

131. An iterator is an object that:

132. `iter()` function returns:

133. `__next__()` method in an iterator raises what when no more items?

134. A generator function is defined using:

135. Generator expressions look like list comprehensions but use:

136. Generators are memory efficient because:

137. Infinite generators must have a condition to stop or they'll:

138. 'yield from' is used to:

139. StopIteration is raised by __next__() to:

140. itertools module provides:

141. A decorator in Python is applied using:

142. Decorators take a function as input and:

143. `@property` is a built-in decorator to create:

144. Context managers use which methods?

145. Using `with open('file') as f:` is an example of:

146. The `contextlib` module provides:

147. Decorators can be used to:

148. Multiple decorators on one function are applied:

149. The `with` statement calls __enter__ when:

150. The main benefit of context managers is:

151. map(function, iterable) returns:

152. filter(function, iterable) keeps elements where:

153. reduce(function, iterable) in functools module does what?

154. A lambda function is defined using:

155. List comprehensions provide a concise way to:

156. Any() returns True if:

157. All() returns True if:

158. Inline if in comprehensions allow:

159. Functions as first-class objects mean:

160. Functional style is often beneficial because:

161. `datetime` module provides:

162. `os` module is commonly used for:

163. `sys.argv` is used to:

164. `math` module provides:

165. `random` module is used to:

166. `json` module allows:

167. `re` module is for:

168. `collections` module provides:

169. `argparse` module helps in:

170. `subprocess` module is used to:

171. A virtual environment in Python is created using:

172. Activating a virtual env on Unix is done by:

173. `requirements.txt` file lists:

174. `pip install -r requirements.txt` does what?

175. `pip freeze` outputs:

176. Virtual environments help by:

177. pip is a package manager for:

178. Upgrading a package with pip:

179. Conda or Poetry are:

180. Dependency conflicts occur when:

181. unittest is:

182. A unittest test case is created by subclassing:

183. Common assertion method in unittest is:

184. pytest allows tests to be simple functions that:

185. Fixtures in pytest are used to:

186. Mocking in tests is used to:

187. Running subset of tests in pytest can be done by:

188. `assertRaises` in unittest is used to:

189. Coverage tools measure:

190. TDD (Test-Driven Development) means:

191. GIL (Global Interpreter Lock) in CPython affects:

192. `multiprocessing` module allows:

193. async/await syntax is used for:

194. Parallelizing CPU-bound tasks can be improved by:

195. Profiling code helps by:

196. `cProfile` is a tool for:

197. Partitioning large datasets can help by:

198. Caching results can improve performance by:

This quiz is for Premium Users only, purchase a premium membership here to access.

199. `functools.lru_cache` decorator can be used to:





This quiz is for Premium Users only, purchase a premium membership here to access.

200. Async IO helps when:





...
Ask Tutor
Tutor Chat