w3resource

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?

  1. 72 characters
  2. 79 characters
  3. 99 characters
  4. 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?

  1. camelCase
  2. snake_case
  3. PascalCase
  4. 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?

  1. camelCase
  2. snake_case
  3. UPPERCASE
  4. 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)

  1. Use tabs
  2. Use 4 spaces per indentation level
  3. Use 2 spaces per indentation level
  4. 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).

  1. Imports should be on separate lines
  2. Imports should be grouped in a specific order
  3. Imports should be at the top of the file
  4. 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?.

  1. camelCase
  2. snake_case
  3. PascalCase
  4. 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?

  1. 2 spaces
  2. 4 spaces
  3. 8 spaces
  4. 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)

  1. Comments should be complete sentences
  2. Comments should be capitalized
  3. Inline comments should be separated by at least two spaces from the statement
  4. 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)

  1. Avoid extraneous whitespace in expressions and statements
  2. Always surround binary operators with a single space on both sides
  3. Use a single space after commas, colons, and semicolons
  4. 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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/literals-and-variables-implementing-PEP-recommendations.php