Python Naming Conventions quiz
Progress
Score: 0/20
๐ฉ Master Quiz: Python Naming
20 Questions. Prove your DevSecOps Expertise! Green = Correct, Red = Incorrect.
BeginnerAnalogy
In the "Dress Code" analogy, what corresponds to a "Sherwani or Suit"?โ
- **A) Python Syntax (The Law)
** - **B) Conventions (Respectful & Clean)
** - **C) The Compiler (The Judge)
** - **D) Hardware (The Venue)
**
๐ก Explanationโ
Conventions.
Just like wearing a suit isn't illegal but is respectful, conventions make your code professional.
BeginnerVariable Style
What is the recommended style for my_variable_name?โ
- **A) Snake Case
** - **B) Camel Case
** - **C) Pascal Case
** - **D) Hungarian Notation
**
๐ก Explanationโ
Snake Case.
Lowercase with underscores (e.g., user_login_count) is the Python standard.
IntermediateClass Naming
Which style is strictly used for Class Names?โ
- **A) snake_case
** - **B) camelCase
** - **C) PascalCase
** - **D) UPPER_CASE
**
๐ก Explanationโ
PascalCase.
Example: StudentData. This allows instant recognition: if it's capitalized, it's a Class.
ArchitectAutomation
Which tool in a CI/CD pipeline automates style checks?โ
- **A) Debugger
** - **B) Linter (e.g., pylint)
** - **C) Compiler
** - **D) Interpreter
**
๐ก Explanationโ
Linters.
Tools like pylint or flake8 automatically reject code that violates PEP 8.
IntermediateConstants
How should you write a constant like "API URL"?โ
- **A) api_url
** - **B) ApiUrl
** - **C) apiUrl
** - **D) API_URL
**
๐ก Explanationโ
UPPER_CASE.
Constants use all caps with underscores, e.g., PI or API_URL.
AdvancedRefactoring
Refactor employeeID to standard Python style.โ
- **A) EmployeeId
** - **B) employee_id
** - **C) employeeid
** - **D) EMPLOYEE_ID
**
๐ก Explanationโ
employee_id (Snake Case).
employeeID is camelCase (Java style). In Python, variables must be snake_case.
IntermediateAnti-Patterns
Why avoid Hungarian Notation (e.g., strName)?โ
- **A) It is too short
** - **B) It uses too much memory
** - **C) Python is dynamic; types change
** - **D) It is illegal syntax
**
๐ก Explanationโ
Python is dynamic.
strName is misleading if the variable later holds a number or object.
BeginnerDescriptive Naming
Which name follows the "Meaning Rule" best?โ
- **A) d = 10
** - **B) days = 10
** - **C) days_since_last_login = 10
** - **D) val = 10
**
๐ก Explanationโ
days_since_last_login.
Code is read more often than it is written. Being explicit saves time for the next developer.
AdvancedLegacy Code
When might you be forced to use camelCase?โ
- **A) In libraries like
unittest``** - **B) For private variables
** - **C) In Python 3.10+
** - **D) Never
**
๐ก Explanationโ
In external/legacy libraries.
Libraries like unittest use Java styles (e.g., setUp). You must match them to use them.
ArchitectSingle Letter Vars
When are variables like x or i acceptable?โ
- **A) For main data storage
** - **B) In short loops or math formulas
** - **C) For database connections
** - **D) Always
**
๐ก Explanationโ
In short loops or math.
Using i for a loop counter is standard. For everything else, be descriptive.
AdvancedPackage Naming
What is the convention for Package names?โ
- **A) short_lowercase
** - **B) PascalCase
** - **C) camelCase
** - **D) UPPERCASE
**
๐ก Explanationโ
Short, all-lowercase names.
Packages (directories) should have short, all-lowercase names. Underscores are discouraged unless necessary.
AdvancedExceptions
How should you name a custom Exception?โ
- **A)
database_crash``** - **B)
DatabaseCrash``** - **C)
DatabaseError``** - **D)
ErrorDatabase``**
๐ก Explanationโ
PascalCase ending with "Error".
Since Exceptions are classes, they use PascalCase. The convention is to suffix them with Error (e.g., ValueError).
ArchitectProtected Members
What does a single underscore prefix (_var) mean?โ
- **A) It is a system variable
** - **B) It is for "Internal Use" (Protected)
** - **C) It is strictly private
** - **D) It cannot be imported
**
๐ก Explanationโ
Internal Use Only (Protected).
This is a polite convention telling other developers: "Please don't access this directly from outside the class."
ArchitectName Mangling
What happens with double underscores (__var)?โ
- **A) Nothing special
** - **B) It becomes global
** - **C) Name Mangling triggers
** - **D) It deletes the variable
**
๐ก Explanationโ
Name Mangling.
Python renames __var to _ClassName__var to prevent subclasses from accidentally overwriting it. It is the closest thing to "Private".
IntermediateKeyword Conflicts
How do you name a variable if it conflicts with a keyword (e.g., class)?โ
- **A)
klass``** - **B)
class_(Trailing Underscore)** - **C)
Class``** - **D)
_class``**
๐ก Explanationโ
Trailing Underscore (class_).
PEP 8 recommends adding a single trailing underscore to avoid conflicts with Python keywords.
BeginnerMethod Arguments
What is the first argument of an instance method?โ
- **A)
self``** - **B)
this``** - **C)
me``** - **D)
obj``**
๐ก Explanationโ
self.
While you could name it anything, using anything other than self is extremely confusing for Python developers.
IntermediateClass Methods
What is the first argument of a @classmethod?โ
- **A)
self``** - **B)
class``** - **C)
cls``** - **D)
type``**
๐ก Explanationโ
cls.
For class methods, the convention is strictly cls. Using self here would be misleading.
AdvancedWhitespace
Which assignment follows PEP 8 whitespace rules?โ
- **A)
x=1``** - **B)
x = 1``** - **C)
x = 1``** - **D)
x =1``**
๐ก Explanationโ
x = 1 (Spaces on both sides).
Always surround assignment operators with a single space to let the code "breathe".
ArchitectGlobal Variables
How do you name a module-level global variable?โ
- **A)
_my_global(if internal) ormy_global``** - **B)
MyGlobal``** - **C)
g_MyGlobal``** - **D)
GLOBAL_VAR``**
๐ก Explanationโ
Snake Case (with optional underscore).
Globals follow the same rules as functions (snake_case). If they are constants, use UPPER_CASE.
IntermediateImport Aliases
Which import alias is standard?โ
- **A)
import pandas as p``** - **B)
import numpy as num``** - **C)
import pandas as pd``** - **D)
import tensorflow as t``**
๐ก Explanationโ
pd for pandas.
Using standard aliases (np, pd, plt) makes your code readable to the entire data science community.
๐ Quiz Complete!โ
Your Final Score:
0/20 Novice
Keep practicing to reach Architect level!
- **โบ Retake Quiz
**