Skip to main content

Python Variables Quiz

ยท 5 min read

Progress

Score: 0/20

๐ŸŽฉ Python Naming Conventions

Prove your DevSecOps expertise! Answer 20 questions to earn your rank. Green = Correct, Red = Incorrect.

BeginnerVariables

  • **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**