Essential Python Naming Conventions for PCEP-30-02 Exam Success
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 naming conventions. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02 examination.
Question 1: Which of the following is a valid variable name according to Python naming conventions?
- 2ndVar
- my-var
- my_Var
- my var
Answer: C) my_Var
Explanation: Variable names in Python cannot start with a digit, contain hyphens or spaces. Using underscores is a valid convention.
Question 2: What is the recommended naming convention for variable names in Python?
- camelCase
- PascalCase
- snake_case
- kebab-case
Answer: C) snake_case
Explanation: Python recommends using snake_case for variable names, where words are separated by underscores.
Question 3: Which of the following variable names adhere to Python's naming conventions? (Select all that apply)
- first_name
- FirstName
- firstName
- first-name
Answer: A) first_name B) FirstName
Explanation: first_name uses snake_case, and FirstName uses PascalCase, which are acceptable conventions. firstName uses camelCase and first-name uses kebab-case, which are not recommended for variables.
Question 4: Match the following naming conventions with their correct descriptions.
Naming Conventions | Descriptions |
---|---|
camelCase | 1. First word lowercase, subsequent words capitalized |
PascalCase | 2. Words separated by hyphens |
snake_case | 3. Words separated by underscores |
kebab-case | 4. Each word capitalized |
Answer:
- camelCase -> 1. First word lowercase, subsequent words capitalized
- PascalCase -> 4. Each word capitalized
- snake_case -> 3. Words separated by underscores
- kebab-case -> 2. Words separated by hyphens
Explanation: Each naming convention matches its respective description.
Question 5: Fill in the blanks: In Python, the recommended naming convention for variable names is ________, while ________ is typically used for class names.
▼Answer: In Python, the recommended naming convention for variable names is snake_case, while PascalCase is typically used for class names.
Explanation: snake_case is recommended for variables, and PascalCase is used for class names.
Question 6: Arrange the following variable names in order according to Python's naming convention (valid to invalid).
Variable Names | Order |
---|---|
myVariable | 1 |
2nd_variable | 2 |
my_variable | 3 |
my-variable | 4 |
Answer:
3 | my_variable |
1 | myVariable |
2 | 2nd_variable |
4 | my-variable |
Explanation: my_variable follows snake_case, myVariable follows camelCase, 2nd_variable starts with a digit, and my-variable contains a hyphen, making the latter two invalid.
Question 7: Fill in the code to define a variable using the recommended naming convention in Python.
user_name = "Alice" print(_______)
▼Answer: user_name
Explanation: The variable user_name follows the snake_case convention and is printed correctly.
Question 8: Insert the missing line of code to define a variable following the snake_case convention.
_______ = "Python" print(programming_language)▼
Answer: programming_language
Explanation: The variable programming_language follows the snake_case convention.
Question 9: What will be the output of the following code?
first_name = "Lois" last_name = "Jennifer" print(first_name, last_name)
- LoisJennifer
- Lois Jennifer
- first_name last_name
- Lois, Jennifer
Answer: B) Lois Jennifer
Explanation: The print function outputs the values of first_name and last_name separated by a space.
Question 10: Which of the following is not an acceptable variable name according to Python naming conventions?
- _hidden_var
- hiddenVar
- hidden-var
- hidden_var
Answer: C) hidden-var
Explanation: Variable names cannot contain hyphens.
Question 11: Which of the following statements about Python naming conventions are true? (Select all that apply)
- Variable names can start with an underscore.
- Variable names should be all uppercase
- Variable names should be descriptive and meaningful
- Variable names can contain letters, digits, and underscores
Answer: A) Variable names can start with an underscore. C) Variable names should be descriptive and meaningful. D) Variable names can contain letters, digits, and underscores.
Explanation: Variable names can start with an underscore, should be descriptive, and can contain letters, digits, and underscores. All uppercase is typically reserved for constants.
Question 12: Match the following naming conventions with their usage.
Naming Conventions | Usage |
---|---|
UPPER_CASE | 1. Variable names |
snake_case | 2. Class names |
PascalCase | 3. Constants |
camelCase | 4. Private variables (less common) |
Answer:
- UPPER_CASE -> 3. Constants
- snake_case -> 1. Variable names
- PascalCase -> 2. Class names
- camelCase -> 4. Private variables (less common)
Explanation: Each naming convention matches its typical usage.
Question 13: Fill in the blanks: Constants in Python are typically written in ________ case, while variables follow the ________ case convention.
▼Answer: Constants in Python are typically written in UPPER case, while variables follow the snake case convention.
Explanation: Constants use uppercase, and variables use snake_case.
Question 14: Arrange the following naming conventions in order of their typical usage: constants, class names, variable names.
Naming Conventions | Order |
---|---|
snake_case | 1 |
UPPER_CASE | 2 |
PascalCase | 3 |
Answer:
- snake_case
- PascalCase
- UPPER_CASE
Explanation: The order is based on the usage for variable names, class names, and constants, respectively.
Question 15: Fill in the code to declare a constant following the UPPER_CASE convention.
_______ = 3.14159 print(PI)▼
Answer: PI
Explanation: The constant PI is declared in uppercase, following the UPPER_CASE convention.
Question 16: Insert the missing line of code to define a class name following the PascalCase convention.
class _______: def __init__(self): pass▼
Answer: MyClass
Explanation: The class MyClass follows the PascalCase convention.
Question 17: What will be the output of the following code?
user_name = "Alice" USER_NAME = "Bob" print(user_name, USER_NAME)
- Alice Alice
- Bob Bob
- Alice Bob
- user_name USER_NAME
Answer: C) Alice Bob
Explanation: The variables user_name and USER_NAME are case-sensitive and hold different values.
Question 18: Which of the following is a valid variable name for a private variable in Python?
- privateVar
- _private_var
- __privateVar
- private-var
Answer:B) _private_var
Explanation: Private variables in Python often start with an underscore (_).
Question 19: Which of the following are valid naming conventions for private variables in Python? (Select all that apply)
- _var
- __var
- var_
- var__
Answer: A) _var B) __var
Explanation: Private variables typically start with a single or double underscore (_var, __var).
Question 20: Match the following types of variables with their naming conventions.
Types of Variables | Naming Conventions |
---|---|
Constant | 1. snake_case |
Class Name | 2. PascalCase |
Variable | 3. UPPER_CASE |
Private Variable | 4. _single_leading_underscore |
Answer:
- Constant -> 3. UPPER_CASE
- Class Name -> 2. PascalCase
- Variable -> 1. snake_case
- Private Variable -> 4. _single_leading_underscore
Explanation: Each type of variable matches its naming convention.
Question 21: Fill in the blanks: Private variables in Python typically start with a ________, while constants are written in ________.
▼Answer: Private variables in Python typically start with a single leading underscore, while constants are written in UPPER_CASE.
Explanation: Private variables use a leading underscore, and constants use uppercase.
Question 22: Arrange the following steps to define and print a variable following the snake_case convention.
Steps | order |
---|---|
my_variable = 42 | 1 |
print(my_variable) | 2 |
Answer:
1 | my_variable = 42 |
2 | print(my_variable) |
Explanation: The sequence shows defining a variable using snake_case and then printing it.
Question 23: Fill in the code to declare a class using the PascalCase convention.
class _______: def __init__(self, name): self.name = name▼
Answer: MyClass
Explanation: The class MyClass follows the PascalCase convention.
Question 24: Insert the missing line of code to define a private variable following the _single_leading_underscore convention.
class Example: def __init__(self): self._______ = "secret"▼
Answer: _private_var
Explanation: The private variable _private_var follows the _single_leading_underscore convention.
Question 25: What will be the output of the following code?
_var = 5 Var = 10 print(_var, Var)
- 5 5
- 10 10
- 5 10
- _var Var
Answer: C) 5 10
Explanation: The variables _var and Var are case-sensitive and hold different values.
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-naming-conventions.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics