25 Python Modules Quiz Questions for Best Practice
Welcome to this ultimate Python modules quiz! Here, we have collected 25 multiple-choice questions based on modules in Python, which will cover basic to advanced concepts. This MCQ quiz will test your knowledge in modules as well as level up your understanding in modules.
Whether youβre a beginner or an experienced Python programmer, this quiz is perfect for coding interviews, competitive programming, and enhancing your knowledge in Python modules. So, are you ready to score 25/25? Letβs see, how well do you really understand modules chapter of Python? Letβs begin with question number 1.π
1. What is a module in Python?
A. A built-in function
B. A folder that stores Python packages.
C. A special data type in Python
D. A file containing a collection of related Python statements and definitions.
d
A module in Python is a file which contains a collection of related Python statements and definitions.
2. How do you import a module named math in Python?
A. include math
B. import math
C. using math
D. load math
b
To import a module, you use the import keyword followed by the module name, like import math.
3. What will import math statement allow you to do?
A. Access all string methods
B. Create custom modules
C. Access mathematical functions like math.sqrt()
D. None
c
The math module provides access to mathematical functions like sqrt(), floor(), ceil(), and constants like pi.
4. What is the output of the following code?
import math print(math.sqrt(16))
A. 2
B. 4
C. 16
D. Error
b
The math.sqrt() function calculates the square root of a number. The square root of 16 is 4.
5. What is the purpose of dir(module_name) in Python?
A. Delete a module
B. Install a module
C. Returns a sorted list of names (attributes, functions, classes, variables) defined in a module.
D. Create a module
c
The dir() function returns a sorted list of names (attributes, functions, classes, variables) defined in a module.
6. What is the extension of a module file in Python?
A. .mod
B. .py
C. .txt
D. .module
b
The extension of a module file in Python is .py. You can import any .py file as a module.
7. Which statement is used to import everything from a module in Python?
A. import all from module
B. import module.*
C. from module import *
D. from module import all
c
The statement from module import * imports all public functions, classes, and variables from the specified module into the current namespace. However, this practice is discouraged in large programs because it can cause namespace conflicts and reduce code clarity.
8. How can you import only the pi constant from the math module?
A. import pi from math
B. from math import pi
C. import math.pi
D. require pi from math
b
The from module import name syntax allows importing specific attributes (like pi) without loading the entire module in the current namespace.
9. What is the purpose of the as keyword when importing a module under an alternate name or with an alias name?
A. To uninstall the module
B. To import private functions
C. To hide the module
D. To create an alias name for a module
d
The as keyword is used to create an alias for a module when importing it. For example, the statement import numpy as np lets you use np.array() instead of numpy.array().
10. Which of the following is the correct way to import only the randint function from the random module?
A. import random.randint
B. from random import randint
C. using random.randint
D. include random -> randint
b
The statement from random import randint imports only the randint function, allowing you to use it directly as randint().
11. What happens if you try to import a non-existent module in Python?
A. Python creates an empty module automatically.
B. Python prompts the user to install the module.
C. Python raises an ImportError.
D. None of the above
c
If a module does not exist, Python raises an ImportError.
12. How can you access a function from a module after importing it using import module_name?
A. function()
B. module_name->function()
C. module_name.function()
D. import.function()
c
When a module is imported using import module_name, you access its functions using dot notation. For example: math.sqrt(16).
13. Which built-in module is used to work with command-line arguments?
A. os
B. subprocess
C. sys
D. platform
c
The sys module in Python provides access to system-specific parameters and functions, including command-line arguments. The sys.argv variable is a list containing the arguments passed to a Python script when it is executed from the command line.
14. Which of the following method is used to reload a previously imported module without restarting the Python shell?
A. reload(module)
B. importlib.reload(module)
C. import module.reload()
D. module.reimport()
b
To reload a module during runtime, use importlib.reload(module) from the importlib module.
15. Which of the following modules helps in serializing Python objects?
A. os
B. sys
C. pickle
D. re
c
The pickle module is used for serializing and deserializing Python objects. This module provides functions to convert Python objects into a byte stream.
16. What is the main function of pip in Python?
A. To execute Python scripts from the command line.
B. To install, upgrade, and manage third-party Python packages/modules.
C. To compile Python code into machine language.
D. To debug Python programs.
b
pip stands for “Pip Installs Packages”. It is the official package manager for Python, which is used to install, upgrade, and manage third-party libraries and packages from the Python Package Index (PyPI). For example, the command “pip install requests” installs the requests library into your Python environment.
17. What does __all__ do in a Python module?
A. Declares global variables.
B. Defines default functions.
C. Imports all modules in the package.
D. Restricts what gets imported with from module import *.
d
__all__ in Python is a list that controls which names will be imported when using the syntax from module import *.
18. Which of the following is used to create a custom module in Python?
A. Define a class and save it.
B. Use pip install.
C. Create a file and save it with .py extension.
D. Modify site-packages.
c
A custom module in Python is simply a file with .py extension that contains Python code (like functions, variables, or classes). You can then import that file into other Python scripts.
19. Which of the following is the function of importlib module in Python?
A. Import modules during runtime.
B. Compress modules.
C. Remove built-in modules.
D. Convert modules to packages.
a
The importlib module allows you to dynamically import modules using the syntax like importlib.import_module(“module_name”).
20. Which of the following is the function of sys.path list in Python?
A. Stores all modules in Python.
B. List of directories that the interpreter will search in for the required module.
C. Holds environment variables.
D. None of the above
b
sys.path is a built-in variable within the sys module. It contains a list of directories where the interpreter searches in for the required module when importing.
21. Which of the following is the primary function of the __init__.py file in a package directory?
A. It defines the main function of the package.
B. It lists all available modules on the system.
C. It marks the directory as a Python package.
D. It compiles the code in the package automatically.
c
In Python, the __init__.py file is used to mark a directory as a Python package, which allows you to import modules from that directory. It can be empty or include package-level initialization code.
22. Consider a module my_module.py with the following code. What is the correct way to call the greet function from another file?
def greet(name):
return f"Hello, {name}!"
A. my_module.greet(“John”)
B. import greet from my_module
C. import my_module then my_module.greet(“John”)
D. greet(“John”) without importing anything
c
When importing a module, you must use the syntax moduleName.function_name() to access its content.
23. Look at the below file structure. If tools.py has a function def add(x, y):, how do you import it in main.py?
project/ βββ utils/ β βββ __init__.py β βββ tools.py
A. import tools.add
B. from utils import add
C. import utils.add
D. from utils.tools import add
d
utils/ is a package (due to __init__.py). tools.py is a module inside the package. from utils.tools import add is the correct way to access a function from a sub-module inside a package. Use dot notation to specify the full path: from package.module import function_name.
24. What is the output of the following code?
import sys print(type(sys.path))
A. <class ‘string’>
B. <class ‘list’>
C. <class ‘tuple’>
D. <class ‘dict’>
b
sys.path is a list of directory strings that Python searches for modules.
25. What does the statement if __name__ == “__main__”: do in Python?
A. Allows code to execute only when the module is run directly as a script.
B. Prevents the module from being imported.
C. Marks the module as deprecated.
D. None of the above
a
In Python, the if __name__ == “__main__”: statement allows code to execute only when the module is run directly as a script, not when it is imported as a module into another script. In simple words, the code inside if __name__ == “__main__”: runs only when the script is executed directly.
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score



