Quiz — Chapter 39: Python Best Practices and Collaborative Development

Select the best answer for each question. Answer key at the end.


1. Which command initializes a new Git repository in the current directory?

A. git start B. git init C. git new D. git create


2. What is the correct order of steps in the standard Git commit workflow?

A. commitaddpush B. pushaddcommit C. addcommitpush D. addpushcommit


3. Which of the following should you include in a .gitignore file for a Python project? (Select all that apply)

A. venv/ B. requirements.txt C. .env D. __pycache__/ E. README.md


4. Priya runs pytest on her project and sees: 5 passed, 1 failed. What does the failing test tell her?

A. Her Python installation is broken B. A specific input/output combination does not produce the expected result C. The test file has a syntax error D. Her code will not run in production


5. Why should you use pytest.approx when testing financial calculations?

A. It makes tests run faster B. It handles floating-point representation imprecision in equality comparisons C. It is required for tests involving currency values D. It automatically rounds results to 2 decimal places


6. What does the following type hint communicate?

def calculate_payback_months(investment: float, monthly_benefit: float) -> float | None:

A. The function accepts only floats and returns a float B. The function might return None in addition to a float C. The function requires mypy to be installed to run D. float | None is a syntax error in Python 3.10+


7. Which tool automatically reformats Python code to a consistent style, making almost no configurable choices?

A. flake8 B. mypy C. black D. pylint


8. What is the primary purpose of mypy?

A. To format Python code automatically B. To check type annotations statically without running the code C. To run tests faster than pytest D. To analyze code coverage


9. Which of the following is a good Git commit message?

A. "fixed it" B. "wip" C. "Add discount calculation with 0% edge case test" D. "changes to the code"


10. What is a pull request on GitHub?

A. A way to download a repository to your local machine B. A request to pull new packages from PyPI C. A formal proposal to merge one branch into another, enabling code review D. A command that syncs your local repository with the remote


11. Maya discovers her invoice calculation function has been adding 1% to every invoice for three months. What type of test would prevent this from happening again?

A. A unit test for the standard case only B. A regression test that explicitly asserts the result does not include any extra percentage C. A type hint on the return value D. A flake8 lint check


12. Which file records the exact versions of all installed packages in a Python project for reproducibility?

A. setup.py B. pyproject.toml C. requirements.txt D. packages.lock


13. In the Google-style docstring convention, what section documents what happens when the function receives invalid input?

A. Notes: B. Warnings: C. Raises: D. Errors:


14. A pre-commit hook is configured with black and flake8. A developer runs git commit. What happens if black reformats a file?

A. The commit succeeds, and the reformatted file is included automatically B. The commit is blocked; the developer must stage the reformatted files and commit again C. flake8 fails instead D. The commit succeeds but a warning is printed


15. What is the recommended project structure location for test files in a professional Python project?

A. In the same directory as the code they test B. In a dedicated tests/ directory at the project root C. In a __tests__ subdirectory inside each source directory D. In a separate repository


16. Which command runs all tests in a tests/ directory with verbose output?

A. pytest -v tests/ B. python test tests/ C. run tests --verbose D. pytest tests/ --all


17. Priya's function calculate_gross_margin currently works correctly. She is about to refactor the function to optimize it. What should she do FIRST?

A. Refactor the function and test it manually with a few values B. Delete the old function and write the new one from scratch C. Confirm the existing tests pass, then refactor, then confirm they still pass D. Ask Marcus to review the refactored version before writing any tests


18. What does a branch in Git allow you to do?

A. Create a backup of your entire repository on GitHub B. Work on changes in an isolated copy without affecting the main codebase C. Revert all changes since the last commit D. Merge two repositories together


19. Which of the following is an edge case worth testing for calculate_gross_margin(revenue, cogs)?

A. Revenue = 874,400, COGS = 550,000 (normal business values) B. Revenue = 0 (potential ZeroDivisionError) C. Revenue = 1,000,000, COGS = 650,000 (different normal values) D. Revenue = 500,000, COGS = 325,000 (another normal case)


20. In code review culture, what is the distinction between a "blocking" and "non-blocking" comment?

A. Blocking comments prevent the file from compiling; non-blocking comments are informational B. Blocking issues must be addressed before the PR can be merged; non-blocking are suggestions that don't require action C. Blocking comments are about code logic; non-blocking are about style only D. There is no standard distinction — all review comments require a response


Answer Key

Q Answer Explanation
1 B git init creates the .git directory that starts tracking changes.
2 C Stage with add, snapshot with commit, upload with push.
3 A, C, D requirements.txt and README.md should be committed. venv/, .env, and __pycache__/ should be ignored.
4 B A failing test precisely identifies which input/output pair produces unexpected results.
5 B Floating-point arithmetic means 0.1 + 0.2 != 0.3 in Python. pytest.approx handles this.
6 B float | None (Python 3.10+) means the function may return a float or None.
7 C black is the opinionated auto-formatter. flake8 is a linter (it reports issues, doesn't fix them).
8 B mypy performs static analysis on type hints without executing the code.
9 C Specific, descriptive, in imperative mood. The others are meaningless.
10 C A PR is a structured request to merge a branch into main, with a built-in review interface.
11 B A regression test named explicitly for the bug ensures the specific failure mode cannot silently return.
12 C pip freeze > requirements.txt captures exact versions for reproducibility.
13 C Raises: documents exceptions the function may raise.
14 B Pre-commit hooks block commits that fail checks. The developer must stage the auto-formatted files and retry.
15 B A dedicated tests/ directory is the standard professional structure.
16 A pytest -v tests/ runs all tests with verbose (per-test) output.
17 C Confirm tests pass → refactor → confirm tests still pass. Tests are the safety net for refactoring.
18 B Branches create isolated workspaces. Changes on a branch don't affect main until merged.
19 B Revenue = 0 is an edge case because it triggers a potential ZeroDivisionError. A, C, D are all normal cases.
20 B Blocking issues gate the merge; non-blocking are suggestions the author may address or decline.