PCPP1 · domain
Best Practices And Coding Conventions
This domain covers PEP 8 style rules, PEP 257 docstring conventions, and secure coding habits tested on the PCPP1 exam. Questions ask you to choose recommended practices for documentation, logging, secret handling, and line length, so you must recall exact PEP guidance and apply it to refactoring scenarios.
Focused practice
Practice Best Practices And Coding Conventions questions
Scored sessions drawing only from this domain — pick a length below.
Start 20-question practice test →What this domain covers
What to know about Best Practices And Coding Conventions
You must apply PEP 8 and PEP 257 correctly and choose secure logging and secret-handling patterns. The single most important thing is to know the exact PEP recommendations for line length, docstrings, and secret storage, then select the option that follows them.
PEP 257 docstring rules for one-line and multi-line function documentation
PEP 8 maximum line length and naming conventions for modules, classes, and functions
Using the logging module instead of print for configurable diagnostic output
Secure handling of secret keys via environment variables or secret managers
Watch out for
Common Best Practices And Coding Conventions exam traps
- ▸Confusing PEP 8 line length with PEP 257 docstring rules, or picking 79 vs 99 without context.
- ▸Assuming print is acceptable for production diagnostics instead of logging with levels and handlers.
- ▸Hardcoding secret keys in source or config files instead of using environment variables or vaults.
Question index
All Best Practices And Coding Conventions questions (53)
Click any question to see the full explanation, or start a practice session above.
Which THREE features are associated with robust logging in production applications?
Hard2In a team environment, which PEP 8 guideline should be applied to maintain consistent indentation when mixing spaces and tabs?
Easy3A developer is writing a Python script that contains a class named `DataProcessor`. According to PEP 8, which naming convention should be used for the class's methods and attributes?
Easy4Which TWO of these are valid PEP 8 recommendations for whitespace?
Easy5You are implementing a custom context manager using a class. Which methods must be implemented to support the 'with' statement?
Hard6A developer is documenting a function `fetch_records` that accepts a database connection, a table name, and an optional limit. The team follows Google Style Python Docstrings. Which approach correctly documents the parameters and their types?
Medium7You are preparing a Python module for PyPI. Your project layout is: project_root/ ├── setup.py ├── src/ │ └── mypackage/ │ ├── __init__.py │ └── core.py └── tests/ └── test_core.py You want to write a test that imports mypackage.core and verifies the function add(a, b). Which import statement should you use at the top of tests/test_core.py to follow best practices for a src-layout project after installing the package in editable mode?
Medium8A developer is reviewing a pull request where a function uses a global variable to track a counter. The reviewer suggests refactoring to avoid global state. Which refactoring best follows best practices for maintainability and testability?
Hard9You are designing a Python class that manages a database connection. The class has a method `_execute_query` that should only be used internally. According to Python best practices and PEP 8, how should you indicate that this method is for internal use only?
Hard10When creating a new project, how should you structure your imports?
Medium11Which TWO practices help maintain clean code when using Python's 'try-except' blocks?
Medium12What is the recommended way to handle a function that requires a large number of arguments?
Medium13According to PEP 257, what is the standard format for a single-line docstring?
Easy14A developer is writing a Python function that accepts a mutable default argument, such as `def add_item(item, items=[])`. A code reviewer flags this as a potential bug. Which statement best explains the issue and the recommended fix according to best practices?
Hard15A developer is writing a Python script and wants to ensure that lines do not exceed the recommended maximum length according to PEP 8. What is the maximum line length that PEP 8 recommends for code?
Easy16A developer is writing a Python script and wants to ensure that the code adheres to PEP 8's recommendations for blank lines. According to PEP 8, how many blank lines should surround a top-level function definition?
Easy17Which TWO practices ensure secure handling of sensitive environment configurations?
Medium18A script processes data from untrusted sources. Which technique best mitigates 'pickle' deserialization vulnerabilities?
Hard19When naming a constant in Python, which convention does PEP 8 recommend?
Easy20Which THREE of the following are recommended in PEP 8 for class definitions?
Easy21A developer is writing a Python script that processes sensitive data. They want to ensure that any errors are logged without exposing the sensitive data itself. Which logging practice should they follow to avoid leaking sensitive information in logs?
Hard22Which THREE practices are recommended when handling external libraries?
Medium23When is it acceptable to ignore PEP 8 guidelines?
Easy24When designing a public API, how should you signal that a method is for internal use only?
Medium25When writing a library, what is the best practice for documenting the usage of a function?
Medium26A developer is writing a Python function that accepts a callback. They want to ensure the function's docstring follows PEP 257 conventions for multi-line docstrings. Which of the following is the correct format for a multi-line docstring?
Hard27In the context of writing robust error handling, what is the 'EAFP' principle?
Hard28A developer is adding a new public function `calculate_discount` to an existing Python module. The module currently uses four spaces per indentation level, and the function spans several nested `if` blocks. Which PEP 8 indentation practice should the developer follow?
Easy29You are refactoring code that performs sensitive operations. Which of the following is the most secure practice for handling secret keys?
Hard30When implementing secure coding practices in a web-facing Python application, how should you handle raw user input to prevent command injection?
Hard31A code reviewer notices that a Python module defines a class named `dataProcessor` and a function named `processData`. The team follows PEP 8. Which naming convention should be applied to the class name?
Medium32You are writing a library that expects a custom exception. Which practice aligns best with Python's exception handling hierarchy?
Medium33Which TWO of these identify a potential 'code smell' in Python exception handling?
Hard34You are writing a Python module that includes a function to compute the factorial of a number. You want to document the function using a docstring that is compatible with the Google Python Style Guide. Which of the following docstrings correctly follows the Google style for documenting parameters and return values, including types?
Hard35A development team is refactoring a legacy Python module. They want to enforce a consistent import ordering convention across the codebase. According to PEP 8, which grouping and ordering of imports is considered best practice?
Medium36A team is reviewing code and notices that a function uses a mutable default argument: def append_to(element, target=[]):. Which change best adheres to Python best practices and avoids unexpected behavior?
Easy37How should one handle the 'KeyboardInterrupt' exception if it must be caught for cleanup purposes?
Hard38You are developing a Python application that uses a third-party library. You need to ensure that your code handles exceptions raised by that library in a way that is both robust and follows best practices. The library raises a custom exception named LibraryError, which is a subclass of Exception. Your function calls a method that may raise LibraryError. Which of the following is the best approach to handle this exception?
Medium39When documenting a complex function, which approach is most compliant with Google Style Python Docstrings regarding parameter types?
Hard40A developer is writing a Python module and wants to follow best practices for documenting functions. Which TWO of the following are recommended by PEP 257 for docstrings? (Choose two.)
Medium41What is the primary benefit of using 'logging' over 'print' statements for application diagnostics?
Medium42According to PEP 8, what is the maximum recommended length for a line of code?
Easy43A developer is writing a function that computes the average of a list of numbers. The function should not modify the input list. Which annotation best communicates this intent and aligns with PEP 484 best practices?
Medium44A developer is writing a module-level variable that holds the maximum number of retries for a network client and will never change during runtime. Which naming convention does PEP 8 recommend for this constant?
Easy45Which comment style is preferred for block comments in Python?
Easy46Which TWO of the following are valid conventions for naming in Python according to PEP 8?
Easy47Which THREE practices improve documentation maintenance?
Medium48A developer is writing a Python module and wants to follow PEP 8's recommendations for imports. The module currently has the following imports: `import os`, `import sys`, `import requests`, `from collections import defaultdict`. According to PEP 8, how should these imports be grouped and ordered?
Medium49Which TWO of the following are secure coding practices in Python?
Hard50A Python developer is writing a function that reads a configuration file and returns a dictionary. They want to follow PEP 8 and best practices for function design and documentation. Which two of the following should they do? (Choose two.)
Medium51A team is reviewing a module where a helper function `_normalize_path` is prefixed with a single underscore. A new developer asks which statement best describes the intent of that naming choice according to common Python conventions.
Medium52You are debugging a legacy application and need to ensure that resources like file handles are always closed. Which construct is preferred?
Medium53A Python developer is reviewing code and notices that a function has a default argument value set to an empty list: `def append_to(element, target=[]):`. According to best practices, what is the recommended way to handle mutable default arguments?
EasyOther domains
All PCPP1 exam domains
Frequently asked questions
- What does the Best Practices And Coding Conventions domain cover on the PCPP1 exam?
- You must apply PEP 8 and PEP 257 correctly and choose secure logging and secret-handling patterns. The single most important thing is to know the exact PEP recommendations for line length, docstrings, and secret storage, then select the option that follows them.
- How many questions are in this domain?
- This page lists all 53 Best Practices And Coding Conventions questions in the PCPP1 question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
- What is the best way to practise this domain?
- Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
- Can I practise only Best Practices And Coding Conventions questions?
- Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.