🎩 Master Quiz: Python Naming
20 Questions. Prove your DevSecOps Expertise!
Green = Correct, Red = Incorrect.
In the “Dress Code” analogy, what corresponds to a “Sherwani or Suit”?
💡 Explanation
Conventions.
Just like wearing a suit isn’t illegal but is respectful, conventions make your code professional.
What is the recommended style for my_variable_name?
💡 Explanation
Snake Case.
Lowercase with underscores (e.g., user_login_count) is the Python standard.
Which style is strictly used for Class Names?
💡 Explanation
PascalCase.
Example: StudentData. This allows instant recognition: if it’s capitalized, it’s a Class.
Which tool in a CI/CD pipeline automates style checks?
💡 Explanation
Linters.
Tools like pylint or flake8 automatically reject code that violates PEP 8.
How should you write a constant like “API URL”?
💡 Explanation
UPPER_CASE.
Constants use all caps with underscores, e.g., PI or API_URL.
Refactor employeeID to standard Python style.
💡 Explanation
employee_id (Snake Case).
employeeID is camelCase (Java style). In Python, variables must be snake_case.
Why avoid Hungarian Notation (e.g., strName)?
💡 Explanation
Python is dynamic.
strName is misleading if the variable later holds a number or object.
Which name follows the “Meaning Rule” best?
💡 Explanation
days_since_last_login.
Code is read more often than it is written. Being explicit saves time for the next developer.
When might you be forced to use camelCase?
💡 Explanation
In external/legacy libraries.
Libraries like unittest use Java styles (e.g., setUp). You must match them to use them.
When are variables like x or i acceptable?
💡 Explanation
In short loops or math.
Using i for a loop counter is standard. For everything else, be descriptive.
What is the convention for Package names?
💡 Explanation
Short, all-lowercase names.
Packages (directories) should have short, all-lowercase names. Underscores are discouraged unless necessary.
How should you name a custom Exception?
💡 Explanation
PascalCase ending with “Error”.
Since Exceptions are classes, they use PascalCase. The convention is to suffix them with Error (e.g., ValueError).
What does a single underscore prefix (_var) mean?
💡 Explanation
Internal Use Only (Protected).
This is a polite convention telling other developers: “Please don’t access this directly from outside the class.”
What happens with double underscores (__var)?
💡 Explanation
Name Mangling.
Python renames __var to _ClassName__var to prevent subclasses from accidentally overwriting it. It is the closest thing to “Private”.
How do you name a variable if it conflicts with a keyword (e.g., class)?
💡 Explanation
Trailing Underscore (class_).
PEP 8 recommends adding a single trailing underscore to avoid conflicts with Python keywords.
What is the first argument of an instance method?
💡 Explanation
self.
While you could name it anything, using anything other than self is extremely confusing for Python developers.
What is the first argument of a @classmethod?
💡 Explanation
cls.
For class methods, the convention is strictly cls. Using self here would be misleading.
Which assignment follows PEP 8 whitespace rules?
💡 Explanation
x = 1 (Spaces on both sides).
Always surround assignment operators with a single space to let the code “breathe”.
How do you name a module-level global variable?
💡 Explanation
Snake Case (with optional underscore).
Globals follow the same rules as functions (snake_case). If they are constants, use UPPER_CASE.
Which import alias is standard?
💡 Explanation
pd for pandas.
Using standard aliases (np, pd, plt) makes your code readable to the entire data science community.