Python Modules and Imports Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering import syntax, namespace management, standard libraries, and module organization.

35 Questions
~70 minutes
1

Question 1

What is the primary purpose of a module in Python?

A
To organize code into logically related parts and promote code reuse.
B
To speed up the execution of the program.
C
To encrypt the source code.
D
To create a graphical user interface.
2

Question 2

Which keyword is used to bring a module into your current script?

A
import
B
include
C
require
D
using
3

Question 3

Analyze the code below. How do you access the `sqrt` function?

python
import math
A
math.sqrt()
B
sqrt()
C
Math.sqrt()
D
import.sqrt()
4

Question 4

What does the syntax `from module import item` do?

A
It imports specific attributes from the module directly into the current namespace.
B
It imports the entire module and requires dot notation.
C
It renames the module.
D
It deletes the item from the module.
5

Question 5

Why is `from module import *` generally discouraged?

A
It pollutes the local namespace and can cause name collisions.
B
It is slower to execute.
C
It imports nothing.
D
It is a syntax error.
6

Question 6

Analyze the code below. What is the correct way to call the function?

python
import datetime as dt
A
dt.date.today()
B
datetime.date.today()
C
date.today()
D
dt.today()
7

Question 7

Which built-in module would you use to interact with the operating system (e.g., file paths, environment variables)?

A
os
B
sys
C
system
D
platform
8

Question 8

Where does Python look for modules when you try to import them?

A
In the list of directories defined in `sys.path`.
B
Only in the current directory.
C
Only in the standard library folder.
D
On the internet.
9

Question 9

Analyze the code below. What happens if `my_module.py` contains `print('Hello')`?

python
import my_module
import my_module
A
'Hello' is printed once.
B
'Hello' is printed twice.
C
Nothing is printed.
D
Error on the second import.
10

Question 10

What is the purpose of the `__name__ == '__main__'` check?

python
if __name__ == '__main__':
    main()
A
To run code only when the script is executed directly, not when imported.
B
To define the main function.
C
To check if the module is part of the standard library.
D
To prevent the module from being imported.
11

Question 11

Which module provides mathematical functions like `sin`, `cos`, and `pi`?

A
math
B
calc
C
numbers
D
algebra
12

Question 12

How do you import two specific functions `func1` and `func2` from `mymodule`?

A
from mymodule import func1, func2
B
import func1, func2 from mymodule
C
import mymodule.func1, mymodule.func2
D
include func1, func2 in mymodule
13

Question 13

What is a package in Python?

A
A directory containing a special `__init__.py` file and other modules.
B
A single .py file.
C
A compiled C extension.
D
A virtual environment.
14

Question 14

Which module allows you to generate random numbers?

A
random
B
rand
C
rng
D
chance
15

Question 15

Analyze the code below. What is `sys.argv`?

python
import sys
print(sys.argv)
A
A list of command-line arguments passed to the script.
B
The version of Python being used.
C
The path to the Python interpreter.
D
A list of installed modules.
16

Question 16

What happens if you try to import a module that is not installed or found?

A
ModuleNotFoundError
B
ImportError
C
NameError
D
ValueError
17

Question 17

How can you list all names defined in a module?

A
dir(module_name)
B
list(module_name)
C
help(module_name)
D
show(module_name)
18

Question 18

Analyze the code below. What is the effect of `del math`?

python
import math
del math
A
It removes the name `math` from the current namespace.
B
It uninstalls the math module from the computer.
C
It deletes the math.py file.
D
It stops the math module from working in other scripts.
19

Question 19

Which statement is true about standard library modules?

A
They are installed automatically with Python.
B
You must download them using pip.
C
They are written only in Python.
D
They are deprecated.
20

Question 20

What is the correct way to import a module named `config` located in a subfolder named `utils`?

A
from utils import config
B
import utils/config
C
import config from utils
D
include utils.config
21

Question 21

Analyze the code below. What is the result?

python
from math import pi as pie
print(pie)
A
3.14159...
B
Error: pie is not defined
C
Error: pi is not defined
D
None
22

Question 22

Which module is used for handling JSON data?

A
json
B
simplejson
C
xml
D
pickle
23

Question 23

What is a circular import?

A
When two or more modules import each other, potentially causing errors.
B
Importing a module in a loop.
C
Importing the same module twice.
D
Importing a module that imports itself.
24

Question 24

How can you reload a module that has already been imported (e.g., during development)?

A
import importlib; importlib.reload(module)
B
import module again
C
reload(module)
D
module.refresh()
25

Question 25

What is the file `__init__.py` used for?

A
To mark a directory as a Python package and initialize it.
B
To start the Python interpreter.
C
To install dependencies.
D
To define the main entry point of a script.
26

Question 26

Which module provides regular expression matching operations?

A
re
B
regex
C
match
D
pattern
27

Question 27

Analyze the code below. What is the issue?

python
from math import *
print(sqrt(4))
print(tan(0))
A
It works, but the namespace is polluted with all math functions.
B
It throws an error because math is not defined.
C
It throws an error because sqrt is not defined.
D
It is the recommended way to import.
28

Question 28

What variable contains the location of the module file?

A
__file__
B
__path__
C
__loc__
D
__dir__
29

Question 29

Which standard module allows you to work with dates and times?

A
datetime
B
time
C
calendar
D
All of the above
30

Question 30

How do you install a third-party module like `requests`?

A
pip install requests
B
import requests
C
python install requests
D
npm install requests
31

Question 31

What is the benefit of using absolute imports over relative imports?

A
They are clearer and less prone to breakage when files are moved.
B
They are shorter to type.
C
They are required by Python 3.
D
They allow circular imports.
32

Question 32

Analyze the code below. What is `m`?

python
import math as m
A
A reference to the math module object.
B
A string 'math'.
C
A copy of the math module.
D
The math class.
33

Question 33

Which module helps in parsing command-line arguments in a user-friendly way?

A
argparse
B
sys
C
getopt
D
cmd
34

Question 34

What does `import sys, os` do?

A
Imports both the sys and os modules.
B
Imports sys as os.
C
Imports os inside sys.
D
Syntax Error.
35

Question 35

Why should imports be placed at the top of the file?

A
It makes dependencies clear and avoids potential errors with scope.
B
Python requires it; code won't run otherwise.
C
It makes the file load faster.
D
It saves memory.

QUIZZES IN Python