Courseiva

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.

53 questions16 easy21 medium16 hard

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.

1

Which THREE features are associated with robust logging in production applications?

Hard
2

In a team environment, which PEP 8 guideline should be applied to maintain consistent indentation when mixing spaces and tabs?

Easy
3

A 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?

Easy
4

Which TWO of these are valid PEP 8 recommendations for whitespace?

Easy
5

You are implementing a custom context manager using a class. Which methods must be implemented to support the 'with' statement?

Hard
6

A 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?

Medium
7

You 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?

Medium
8

A 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?

Hard
9

You 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?

Hard
10

When creating a new project, how should you structure your imports?

Medium
11

Which TWO practices help maintain clean code when using Python's 'try-except' blocks?

Medium
12

What is the recommended way to handle a function that requires a large number of arguments?

Medium
13

According to PEP 257, what is the standard format for a single-line docstring?

Easy
14

A 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?

Hard
15

A 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?

Easy
16

A 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?

Easy
17

Which TWO practices ensure secure handling of sensitive environment configurations?

Medium
18

A script processes data from untrusted sources. Which technique best mitigates 'pickle' deserialization vulnerabilities?

Hard
19

When naming a constant in Python, which convention does PEP 8 recommend?

Easy
20

Which THREE of the following are recommended in PEP 8 for class definitions?

Easy
21

A 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?

Hard
22

Which THREE practices are recommended when handling external libraries?

Medium
23

When is it acceptable to ignore PEP 8 guidelines?

Easy
24

When designing a public API, how should you signal that a method is for internal use only?

Medium
25

When writing a library, what is the best practice for documenting the usage of a function?

Medium
26

A 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?

Hard
27

In the context of writing robust error handling, what is the 'EAFP' principle?

Hard
28

A 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?

Easy
29

You are refactoring code that performs sensitive operations. Which of the following is the most secure practice for handling secret keys?

Hard
30

When implementing secure coding practices in a web-facing Python application, how should you handle raw user input to prevent command injection?

Hard
31

A 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?

Medium
32

You are writing a library that expects a custom exception. Which practice aligns best with Python's exception handling hierarchy?

Medium
33

Which TWO of these identify a potential 'code smell' in Python exception handling?

Hard
34

You 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?

Hard
35

A 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?

Medium
36

A 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?

Easy
37

How should one handle the 'KeyboardInterrupt' exception if it must be caught for cleanup purposes?

Hard
38

You 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?

Medium
39

When documenting a complex function, which approach is most compliant with Google Style Python Docstrings regarding parameter types?

Hard
40

A 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.)

Medium
41

What is the primary benefit of using 'logging' over 'print' statements for application diagnostics?

Medium
42

According to PEP 8, what is the maximum recommended length for a line of code?

Easy
43

A 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?

Medium
44

A 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?

Easy
45

Which comment style is preferred for block comments in Python?

Easy
46

Which TWO of the following are valid conventions for naming in Python according to PEP 8?

Easy
47

Which THREE practices improve documentation maintenance?

Medium
48

A 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?

Medium
49

Which TWO of the following are secure coding practices in Python?

Hard
50

A 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.)

Medium
51

A 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.

Medium
52

You are debugging a legacy application and need to ensure that resources like file handles are always closed. Which construct is preferred?

Medium
53

A 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?

Easy

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.
Python Institute Certified Professional in Python Programming 1 (PCPP1, PCPP-32-101) (PCPP1) Best Practices And Coding Conventions Practice Questions