POC Modules, Libraries & Debugging 2 — Questions and Answers
Question 1: Which statement correctly imports only the `sqrt` function from the `math` module?
- import math.sqrt
- from math import sqrt (Correct answer)
- include math.sqrt
- import sqrt from math
Correct answer: from math import sqrt
The `from module import name` syntax imports a specific name directly into the current namespace.
Question 2: What does `sys.argv[0]` contain when running a Python script?
- The first command-line argument passed by the user
- The name of the script file (Correct answer)
- The Python version string
- The current working directory
Correct answer: The name of the script file
`sys.argv[0]` always holds the script's filename (or an empty string in interactive mode).
Question 3: What is the purpose of the `__all__` list defined inside a module?
- Lists all classes in the module
- Controls what is exported when `from module import *` is used (Correct answer)
- Prevents the module from being imported twice
- Stores module metadata such as version
Correct answer: Controls what is exported when `from module import *` is used
`__all__` explicitly declares which names are exported during a wildcard import.
Question 4: Which built-in function returns a list of names defined in the current namespace or a given object?
- vars()
- globals()
- dir() (Correct answer)
- inspect()
Correct answer: dir()
`dir()` returns a sorted list of names in the current scope or the attributes of the passed object.
Question 5: In the `logging` module, which level is HIGHER in severity than WARNING?
- INFO
- DEBUG
- NOTICE
- ERROR (Correct answer)
Correct answer: ERROR
The logging severity order is DEBUG < INFO < WARNING < ERROR < CRITICAL.
Question 6: What exception is raised when you try to import a module that does not exist?
- ImportError
- ModuleNotFoundError (Correct answer)
- NameError
- FileNotFoundError
Correct answer: ModuleNotFoundError
`ModuleNotFoundError` (a subclass of `ImportError`) is raised when Python cannot locate the specified module.
Question 7: Which `pdb` command prints the value of a variable while debugging?
- show
- print / p (Correct answer)
- inspect
- eval
Correct answer: print / p
In pdb, the `p expression` command evaluates and prints the value of an expression.
Which statement correctly imports only the `sqrt` function from the `math` module?