0
0
Pythonprogramming~20 mins

Import statement behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Import Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of importing a module with side effects
What is the output when running this code?
Python
import math
print(math.pi)
import math
print(math.sqrt(16))
A
4.0
4.0
B
3.141592653589793
3.141592653589793
C
3.141592653589793
4.0
DError: module math cannot be imported twice
Attempts:
2 left
💡 Hint
Remember that importing a module twice does not reload it.
Predict Output
intermediate
2:00remaining
Effect of import * on namespace
What is the output of this code?
Python
from math import *
print(sin(0))
print(cos(0))
A
sin
cos
B
0
0
CError: sin is not defined
D
0.0
1.0
Attempts:
2 left
💡 Hint
import * brings all functions into current namespace.
Predict Output
advanced
2:00remaining
Importing a module with circular dependency
Given two modules a.py and b.py where each imports the other, what error occurs when running a.py?
Python
a.py:
import b
x = 5

b.py:
import a
y = 10
AAttributeError: partially initialized module 'a' has no attribute 'x'
BNo error, runs fine
CSyntaxError: circular import not allowed
DImportError: cannot import name 'a' from 'b'
Attempts:
2 left
💡 Hint
Circular imports without accessing attributes during module initialization do not cause errors.
Predict Output
advanced
2:00remaining
Effect of import aliasing on variable access
What is the output of this code?
Python
import math as m
print(m.sqrt(25))
print(math.sqrt(25))
A
5.0
NameError: name 'math' is not defined
B
5.0
5.0
C
NameError: name 'm' is not defined
5.0
DError: cannot use alias and original name together
Attempts:
2 left
💡 Hint
Aliasing changes the module name in current scope.
🧠 Conceptual
expert
2:00remaining
Understanding import caching behavior
Which statement best describes Python's import caching behavior?
AModules are loaded once and cached in sys.modules for reuse.
BModules cannot be imported more than once in a program.
CModules are loaded from disk every time they are imported.
DModules are reloaded automatically if their source code changes.
Attempts:
2 left
💡 Hint
Think about how Python avoids repeated work when importing.