Python Variables Quiz
Progress
Score: 0/20
π© Python Naming Conventions
Prove your DevSecOps expertise! Answer 20 questions to earn your rank. Green = Correct, Red = Incorrect.
BeginnerVariables
What is the recommended style for variable names in Python?β
- **A) CamelCase
** - **B) PascalCase
** - **C) snake_case
** - **D) UPPER_CASE
**
π‘ Explanationβ
Snake Case (snake_case) is the Python standard.
According to PEP 8, variables and function names should be lowercase with underscores to separate words.
IntermediateClass Naming
When should you strictly use PascalCase?β
- **A) For Function Names
** - **B) For Class Names
** - **C) For Constants
** - **D) For Private Variables
**
π‘ Explanationβ
For Class Names (e.g., BankAccount).
This helps developers instantly recognize that BankAccount is a blueprint (Class), whereas bank_account is an instance.
BeginnerStandards
What is PEP 8?β
- **A) A Python Compiler
** - **B) A Testing Tool
** - **C) The Official Style Guide
** - **D) A Database Driver
**
π‘ Explanationβ
The "Bible" of Python Styling.
PEP 8 (Python Enhancement Proposal 8) defines the standard rules for formatting Python code.
IntermediateConstants
How should you name a constant value like PI?β
- **A) pi
** - **B) Pi
** - **C) PI
** - **D) _pi
**
π‘ Explanationβ
UPPER_CASE (e.g., PI, MAX_RETRIES).
Capitalizing every letter signals: "This value is fixed. Do not change it."
ArchitectAutomation
Which tool automatically checks your code for style violations?β
- **A) Python Tutor
** - **B) A Linter (e.g., pylint)
** - **C) The Compiler
** - **D) Git
**
π‘ Explanationβ
A Linter (like pylint or flake8).
In DevSecOps pipelines, linters reject code that doesn't follow PEP 8 before it reaches production.
AdvancedAnti-Patterns
Why avoid Hungarian Notation (e.g., strName)?β
- **A) Python is dynamically typed
** - **B) It uses too much memory
** - **C) It is illegal syntax
** - **D) Python doesn't support strings
**
π‘ Explanationβ
Because types can change.
Since Python is dynamic, strName could technically be assigned a number later, making the name misleading.
BeginnerDescriptive Naming
Which variable name is preferred?β
- **A)
d = 10``** - **B)
days = 10``** - **C)
days_since_login = 10``** - **D)
i = 10``**
π‘ Explanationβ
days_since_login
Style is important, but meaning is critical. d is vague. The full name explains exactly what the data represents.
AdvancedExceptions
When is it acceptable to use CamelCase (e.g., setUp)?β
- **A) Always
** - **B) Never
** - **C) In legacy libs (unittest)
** - **D) For private variables
**
π‘ Explanationβ
When using legacy libraries.
Libraries like unittest were ported from Java, so they use CamelCase. You must match their style when using them.
BeginnerRefactoring
Refactor calculate_Salary to Python style:β
- **A)
CalculateSalary``** - **B)
calculate_salary``** - **C)
calculateSalary``** - **D)
CALCULATE_SALARY``**
π‘ Explanationβ
calculate_salary (Snake Case).
Functions should always use lowercase letters with underscores.
IntermediateSingle Letter Vars
When are single-letter vars (i, x) okay?β
- **A) For database keys
** - **B) In short loops/math formulas
** - **C) For global variables
** - **D) Never
**
π‘ Explanationβ
In short loops or math.
Using i for loop counters or x, y for coordinates is standard. For anything else, be descriptive.
AdvancedPrivate Variables
What does a single underscore prefix mean (e.g., _data)?β
- **A) It is a System Variable
** - **B) It is for "Internal Use" (Hint)
** - **C) It cannot be accessed at all
** - **D) It makes the variable constant
**
π‘ Explanationβ
"Internal Use Only" (Convention).
Python doesn't have true private variables. A single underscore is a polite note to other developers: "Please don't touch this from outside the class."
ArchitectName Mangling
What happens with double underscores (e.g., __password)?β
- **A) It becomes public
** - **B) It deletes the variable
** - **C) Python changes its name (Mangling)
** - **D) It throws an error
**
π‘ Explanationβ
Name Mangling triggers.
Python internally renames __password to _ClassName__password to prevent subclasses from accidentally overwriting it. It is the closest thing to "Private" in Python.
IntermediateShadowing
Why is list = [1, 2, 3] a bad variable name?β
- **A) Lists must be empty
** - **B) It shadows the built-in
list()``** - **C) Variable names must be 5 chars
** - **D) It causes a Memory Leak
**
π‘ Explanationβ
It shadows a built-in function.
If you name your variable list, you overwrite Python's actual list() function. Later, if you try to convert a tuple to a list using list((1,2)), your code will crash.
IntermediateModules
What is the convention for Module names (file names)?β
- **A) short_lowercase.py
** - **B) PascalCase.py
** - **C) camelCase.py
** - **D) UPPERCASE.py
**
π‘ Explanationβ
Short, all-lowercase names.
Modules should have short, all-lowercase names (e.g., numpy, requests). Underscores can be used if it improves readability, but short is better.
BeginnerReadability
Which single letter variable should be avoided due to font issues?β
- **A) x
** - **B) z
** - **C) l (lowercase L)
** - **D) a
**
π‘ Explanationβ
Lowercase L (l), Uppercase O (O).
In many fonts, l looks exactly like the number 1, and O looks like 0. Using them as variable names confuses other developers.
AdvancedWhitespace
Which assignment follows PEP 8 whitespace rules?β
- **A)
x=1``** - **B)
x = 1``** - **C)
x = 1``** - **D)
x= 1``**
π‘ Explanationβ
x = 1 (One space on each side).
PEP 8 recommends surrounding assignment operators with a single space to let the code "breathe."
ArchitectError Naming
How should you name a custom Exception class?β
- **A)
DatabaseCrash``** - **B)
database_error``** - **C)
DatabaseError``** - **D)
ErrorDatabase``**
π‘ Explanationβ
Suffix with 'Error' (PascalCase).
Exceptions are Classes, so they use PascalCase. The convention is to end the name with "Error" (e.g., ValueError, KeyError, MyCustomError).
IntermediateArguments
What is the correct style for Function Arguments?β
- **A)
def func(my_arg):``** - **B)
def func(MyArg):``** - **C)
def func(myArg):``** - **D)
def func(ARG):``**
π‘ Explanationβ
Snake Case (my_arg).
Function arguments follow the same rule as variables: lowercase with underscores.
AdvancedLine Length
According to strict PEP 8, what is the max line length?β
- **A) 120 chars
** - **B) 79 chars
** - **C) 100 chars
** - **D) Unlimited
**
π‘ Explanationβ
79 Characters.
While modern teams often use 100 or 120, the strict PEP 8 standard is 79 characters. This dates back to when terminals were only 80 columns wide.
ArchitectImports
Which import style is preferred?β
- **A)
import my_lib``** - **B)
from my_lib import *``** - **C)
import MyLib``** - **D)
import numpy as np(Standard Alias)**
π‘ Explanationβ
Explicit Aliases (e.g., np for numpy).
Avoid import * because it pollutes the namespace (variables might clash). Using standard aliases like pd for pandas or np for numpy is best practice.
π Quiz Complete!β
Your Final Score:
0/20 Novice
Keep practicing to reach Architect level!
- **βΊ Retake Quiz
**