Implementing PEP-8 Recommendations for the PCEP-30-02 Exam
PCEP Certification Practice Test - Questions, Answers and Explanations
This comprehensive set of questions and explanations covers the fundamental topic of introducing literals and variables into code and using different numeral systems, focusing on implementing PEP-8 recommendations. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02 examination.
Question 1: According to PEP-8, what is the maximum recommended line length for a Python file?
- 72 characters
- 79 characters
- 99 characters
- 120 characters
Answer: b) 79 characters
Explanation: PEP-8 recommends a maximum line length of 79 characters.
Question 2: How should variable names be written according to PEP-8?
- camelCase
- snake_case
- PascalCase
- UPPERCASE
Answer: b) snake_case
Explanation: PEP-8 recommends using snake_case for variable names.
Question 3: According to PEP-8, how should constants be named?
- camelCase
- snake_case
- UPPERCASE
- PascalCase
Answer: c) UPPERCASE
Explanation: PEP-8 recommends using UPPERCASE for constants.
Question 4: Which of the following are recommended by PEP-8 for indentation? (Select all that apply)
- Use tabs
- Use 4 spaces per indentation level
- Use 2 spaces per indentation level
- Mix tabs and spaces
Answer:b) Use 4 spaces per indentation level
Explanation: PEP-8 recommends using 4 spaces per indentation level and advises against using tabs or mixing tabs and spaces.
Question 5: Which of the following are true about PEP-8 recommendations for imports? (Select all that apply).
- Imports should be on separate lines
- Imports should be grouped in a specific order
- Imports should be at the top of the file
- Use wildcard imports
Answer: a) Imports should be on separate lines, b) Imports should be grouped in a specific order, c) Imports should be at the top of the file
Explanation: PEP-8 recommends that imports should be on separate lines, grouped in a specific order (standard library imports, related third-party imports, local application/library-specific imports), and should be at the top of the file. Wildcard imports should be avoided.
Question 6: Place the following import statements in the recommended order according to PEP-8: import os, import mymodule, import sys.
▼Answer:
- import os
- import sys
- import mymodule
Explanation: PEP-8 recommends ordering imports as: standard library imports first, then third-party imports, and finally local application/library-specific imports.
Question 7: Place the following variable names in the recommended format according to PEP-8: MyVariable, my_variable, MY_VARIABLE, myVariable.
▼Answer:
- my_variable
- MyVariable
- MY_VARIABLE
- myVariable
Explanation: According to PEP-8, variable names should be in snake_case, constants in UPPERCASE, and class names in CamelCase.
Question 8: The recommended maximum line length in PEP-8 is ______ characters.
▼Answer: 79
Explanation: PEP-8 recommends a maximum line length of 79 characters.
Question 9: According to PEP-8, function names should be written in ______.
▼Answer: snake_case
Explanation: PEP-8 recommends using snake_case for function names.
Question 10: Sort the following types of imports in the order PEP-8 recommends: standard library imports, local application/library-specific imports, related third-party imports.
▼Answer:
- standard library imports
- related third-party imports
- local application/library-specific imports
Explanation: PEP-8 recommends ordering imports as: standard library imports first, then related third-party imports, and finally local application/library-specific imports.
Question 11: Sort the following elements in the recommended format according to PEP-8: CONSTANT_NAME, variable_name, ClassName, function_name.
▼Answer:
- ClassName
- CONSTANT_NAME
- function_name
- variable_name
Explanation: According to PEP-8, constants should be in UPPERCASE, variable names and function names should be in snake_case, and class names should be in CamelCase.
Question 12: Fill in the missing code to correctly format a function definition according to PEP-8.
def my_function(______, arg2): pass▼
Answer:arg1
Explanation: Function arguments should be written in snake_case.
Question 13: Fill in the missing code to correctly import modules according to PEP-8.
import ______ import os import mymodule▼
Answer: sys
Explanation: Imports should be in alphabetical order within their group.
Question 14: Insert the correct code to define a constant according to PEP-8.
______ = 100▼
Answer: MAX_LIMIT
Explanation: Constants should be named using UPPERCASE with underscores.
Question 15: Insert the correct code to define a class according to PEP-8.
class ______: pass▼
Answer: MyClass
Explanation: Class names should be written in CamelCase.
Question 16: According to PEP-8, how should a method name be written?.
- camelCase
- snake_case
- PascalCase
- UPPERCASE
Answer: b) snake_case
Explanation: PEP-8 recommends using snake_case for method names.
Question 17: What is the recommended indentation size in PEP-8?
- 2 spaces
- 4 spaces
- 8 spaces
- Tabs
Answer: b) 4 spaces
Explanation: PEP-8 recommends using 4 spaces per indentation level.
Question 18: Which of the following are true about comments according to PEP-8? (Select all that apply)
- Comments should be complete sentences
- Comments should be capitalized
- Inline comments should be separated by at least two spaces from the statement
- Use hashtags for block comments
Answer:a) Comments should be complete sentences, b) Comments should be capitalized, c) Inline comments should be separated by at least two spaces from the statement
Explanation: PEP-8 recommends that comments should be complete sentences, capitalized, and inline comments should be separated by at least two spaces from the statement. Block comments should use the # symbol, but not be referred to as hashtags.
Question 19: According to PEP-8, which of the following statements about whitespace in expressions and statements is true? (Select all that apply)
- Avoid extraneous whitespace in expressions and statements
- Always surround binary operators with a single space on both sides
- Use a single space after commas, colons, and semicolons
- Use spaces around assignment operators
Answer: a) Avoid extraneous whitespace in expressions and statements, b) Always surround binary operators with a single space on both sides, c) Use a single space after commas, colons, and semicolons
Explanation: PEP-8 recommends avoiding extraneous whitespace in expressions and statements, surrounding binary operators with a single space on both sides, and using a single space after commas, colons, and semicolons. However, spaces around assignment operators are generally used for alignment purposes.
Question 20: According to PEP-8, class names should be written in ______.
▼Answer: CamelCase
Explanation: PEP-8 recommends using CamelCase for class names.
Question 21: The recommended way to write a multi-line comment in PEP-8 is using ______.
▼Answer: triple double-quotes (""")
Explanation: PEP-8 recommends using triple double-quotes for multi-line comments.
Question 22: Sort the following lines of code to follow PEP-8's recommendations for function definitions.
def my_function(x, y): # Function to add two numbers return x + y▼
Answer:
def my_function(x, y): """Function to add two numbers""" return x + y
Explanation: PEP-8 recommends using triple double-quotes for docstrings and maintaining proper indentation.
Question 23: Sort the following elements in the order they should appear in a Python file according to PEP-8: class definition, module-level dunder names, module-level imports, function definition.
▼Answer:
- module-level imports
- module-level dunder names
- class definition
- function definition
Explanation: PEP-8 recommends ordering elements in a Python file as imports, module-level dunder names, class definitions, and function definitions.
Question 24: Fill in the missing code to correctly align variable assignments according to PEP-8.
x = 1 yyyyyy = 2 z = 3▼
Answer: Aligning assignment operators in this way is discouraged by PEP-8, which suggests not to use extra whitespace
Explanation: PEP-8 recommends avoiding extraneous whitespace in expressions and statements.
Question 25: Fill in the missing code to correctly define a multi-line string according to PEP-8.
long_string = (______ "This is a very long string that needs to be split " "into multiple lines for better readability.")▼
Answer: """ """
Explanation: PEP-8 recommends using parentheses for implicit line joining inside parentheses, brackets, and braces.
Question 26: Insert the correct code to define a module-level constant according to PEP-8.
______ = 3.14▼
Answer: PI
Explanation: Constants should be named using UPPERCASE with underscores.
Question 27: Insert the correct code to define an inline comment according to PEP-8.
x = x + 1 ______ Increment x by 1▼
Answer: #
Explanation: PEP-8 recommends that inline comments should be separated by at least two spaces from the statement.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics