POC Modules, Libraries & Debugging 3 — Questions and Answers
Question 1: What does the `os.path.join()` function do?
- Concatenates strings with a dot separator
- Builds file paths using the OS-appropriate separator (Correct answer)
- Joins two lists of directory entries
- Merges two dictionaries of environment variables
Correct answer: Builds file paths using the OS-appropriate separator
`os.path.join()` constructs a path string using the correct separator for the current operating system.
Question 2: Which module provides the `defaultdict` and `Counter` data structures?
- itertools
- functools
- collections (Correct answer)
- dataclasses
Correct answer: collections
`collections` is the standard library module that contains `defaultdict`, `Counter`, `OrderedDict`, and `deque`.
Question 3: What is a Python package?
- A single `.py` file containing functions
- A directory containing an `__init__.py` file and modules (Correct answer)
- A compressed `.zip` archive of scripts
- A virtual environment folder
Correct answer: A directory containing an `__init__.py` file and modules
A package is a directory that contains an `__init__.py` file, allowing it to be imported as a namespace.
Question 4: How do you set a conditional breakpoint in `pdb` that only triggers when `x > 10`?
- break if x > 10
- condition 1 x > 10 (Correct answer)
- watch x > 10
- tbreak x > 10
Correct answer: condition 1 x > 10
The `condition bpnumber expression` command in pdb applies a condition to an existing breakpoint number.
Question 5: Which `logging` function call creates a message at the DEBUG level?
- logging.info('msg')
- logging.warn('msg')
- logging.debug('msg') (Correct answer)
- logging.trace('msg')
Correct answer: logging.debug('msg')
`logging.debug()` logs a message with severity DEBUG, the lowest standard level.
Question 6: What does `importlib.reload(module)` do?
- Deletes and reinstalls the module from PyPI
- Re-executes the module's code and updates its namespace in place (Correct answer)
- Creates a deep copy of the module object
- Clears the module's `__all__` list
Correct answer: Re-executes the module's code and updates its namespace in place
`importlib.reload()` re-runs the module's source code, refreshing its contents without removing existing references.
Question 7: Which attribute of a module stores its file path on disk?
- __name__
- __path__
- __file__ (Correct answer)
- __origin__
Correct answer: __file__
`module.__file__` contains the absolute path to the `.py` (or `.pyc`) file for that module.
What does the `os.path.join()` function do?