PACKAGES & PIP|TEST BANK- 250 QUESTIONS
WITH CORRECT ANSWERS AND RATIONALES
2026/2027
---
TOPIC A: Basic Import Mechanics (Questions 1-25)
1. What is the primary purpose of the `import` statement in Python?
- A) To execute a system shell command
- B) To bring a module's namespace into the current namespace
- C) To delete an unused variable
- D) To compile Python bytecode
Correct Answer: B
Rationale: The `import` statement loads a module and makes its objects
(functions, classes, variables) available to the current script, effectively
bringing its namespace into scope.
2. Which syntax correctly imports only the `sqrt` function from the `math`
module?
- A) `import sqrt from math`
- B) `from math import sqrt`
- C) `import math.sqrt`
,- D) `include math.sqrt`
Correct Answer: B
Rationale: The `from ... import ...` syntax is specifically designed to import
specific attributes directly into the current namespace without importing the
entire module.
3. What happens when you use `import math` and then try to call `sqrt(9)`
without prefixing it?
- A) It prints 3.0 successfully
- B) It raises a `NameError` because `sqrt` is not defined in the global
namespace
- C) It automatically imports sqrt as well
- D) It raises a `SyntaxError`
Correct Answer: B
Rationale: When you use `import math`, the `sqrt` function is bound to the
`math` namespace. You must call it as `math.sqrt(9)`. Calling `sqrt(9)`
searches the global namespace and fails.
4. Which statement allows you to call `floor(3.7)` directly without a prefix?
- A) `import math`
- B) `import math as m`
- C) `from math import floor`
- D) `from math import *`
Correct Answer: C (and technically D, but C is the specific correct practice)
,Rationale: `from math import floor` binds the `floor` function directly to the
global namespace, allowing direct calls. While `from math import *` also
does this, it is generally discouraged.
5. What is the effect of the `as` keyword in an import statement (e.g.,
`import numpy as np`)?
- A) It copies the module to a new file named `np.py`
- B) It creates an alias (shortcut) for the module name
- C) It validates the module syntax
- D) It installs the module globally
Correct Answer: B
Rationale: The `as` keyword allows you to assign a different name (alias) to
the imported module, which is useful for shortening long module names or
avoiding naming conflicts.
6. Which of the following is a syntax error?
- A) `import math, sys`
- B) `from math import pi, e`
- C) `import math as m`
- D) `from math import as m`
Correct Answer: D
Rationale: The syntax `from math import as m` is invalid. `as` is used to
alias the module itself (e.g., `import math as m`) or an imported function
(e.g., `from math import sqrt as sq`). The syntax in D is missing the function
name before `as`.
, 7. How does Python handle importing the same module multiple times in a
single script?
- A) It executes the module code multiple times
- B) It raises an `ImportError` on the second attempt
- C) It imports it once and caches it in `sys.modules`
- D) It creates a separate copy for each import
Correct Answer: C
Rationale: Python uses a caching mechanism. The first import executes the
module code; subsequent imports simply retrieve the cached module object
from `sys.modules`, preventing redundant execution.
8. Which built-in function returns a list of all names defined in the current
local namespace?
- A) `locals()`
- B) `globals()`
- C) `dir()`
- D) `vars()`
Correct Answer: C
Rationale: While `locals()` and `globals()` return dictionaries, `dir()` without
arguments returns a sorted list of names in the current local scope. When
used with a module argument, it lists the module's attributes.
9. What will `dir(math)` return after importing `math`?
- A) A list of all file names in the math directory