Python Assigning Values Quiz
Progress
Score: 0/15
๐ฉ Master Quiz: Assignments & Memory
Prove your "Architect" level understanding of Python's memory model. Green = Correct, Red = Incorrect.
BeginnerConceptual Analogy
In Python, how is a variable best described compared to languages like C++?โ
- **A) A storage box
** - **B) A sticky tag or label
** - **C) A hard disk sector
** - **D) A constant value
**
๐ก Explanationโ
A sticky tag or label.
Unlike C++ where a variable is a "box" holding data, in Python, the data exists in memory and the variable is just a "name tag" attached to it.
BeginnerDynamic Typing
What does "Dynamic Typing" mean in Python?โ
- **A) You must declare types (int x)
** - **B) Variables cannot change type
** - **C) Types are determined automatically at runtime
** - **D) It only supports strings
**
๐ก Explanationโ
Types are determined automatically.
You don't need to declare int or string. Python figures it out when you assign the value, and a variable can change types later.
IntermediateReference Copying
If you run y = x, what actually happens in memory?โ
- **A) It copies the object inside 'x'
** - **B) It adds a new tag 'y' to the same object
** - **C) It deletes 'x' and creates 'y'
** - **D) It creates a pointer to a pointer
**
๐ก Explanationโ
It adds a new tag to the same object.
You aren't copying the "box"; you are just attaching another label to the existing object in memory.
IntermediateChained Assignment
What is the primary use case for x = y = z = 0?โ
- **A) Initializing counters or flags
** - **B) Creating three distinct zero objects
** - **C) Swapping values
** - **D) Unpacking a list
**
๐ก Explanationโ
Initializing counters/defaults.
This is useful when you want multiple variables to start with the same value (pointing to the same object).
ArchitectGarbage Collection
When does Python's Garbage Collector remove an object?โ
- **A) When the program ends
** - **B) Every 5 minutes
** - **C) When its reference count hits zero
** - **D) When you type
delete()``**
๐ก Explanationโ
When reference count hits zero.
Python tracks how many "tags" are on an object. If you remove all tags (variables), the object is cleaned up to free memory.
IntermediateUnpacking
What is the value of b in: a, b, c = "Orange", "Banana", "Cherry"?โ
- **A) "Orange"
** - **B) "Banana"
** - **C) "Cherry"
** - **D) None
**
๐ก Explanationโ
"Banana".
This is positional assignment. The second variable b gets the second value "Banana".
AdvancedStar Operator
In first, *rest = [1, 2, 3, 4], what is rest?โ
- **A) 2
** - **B)
(2, 3, 4)(Tuple)** - **C)
[2, 3, 4](List)** - **D)
[1, 2, 3, 4]``**
๐ก Explanationโ
[2, 3, 4] (List).
The * operator grabs all "remaining" items and packs them into a list.
BeginnerSwapping
What is the "Pythonic" way to swap two variables?โ
- **A)
temp = x; x = y; y = temp``** - **B)
x, y = y, x``** - **C)
swap(x, y)``** - **D)
x = y``**
๐ก Explanationโ
x, y = y, x
Python's tuple unpacking allows you to swap values in a single line without needing a temporary variable.
ArchitectSystem Tools
Which function lets you check the reference count of an object?โ
- **A)
sys.getrefcount()``** - **B)
sys.memory_usage()``** - **C)
os.count_refs()``** - **D)
inspect.refs()``**
๐ก Explanationโ
sys.getrefcount()
This function from the sys module allows you to peek under the hood and see how many variables reference an object.
AdvancedErrors
What happens if you run a, b = [1, 2, 3]?โ
- **A)
btakes the last two items** - **B)
ValueError: too many values to unpack``** - **C) The 3 is ignored
** - **D)
IndexError``**
๐ก Explanationโ
ValueError.
Unless you use the star operator (*), the number of variables on the left must match the number of items on the right exactly.
AdvancedImmutability
Why does my_tuple[0] = 5 fail?โ
- **A) Tuples are immutable
** - **B) The index is out of bounds
** - **C) Assignment requires unpacking
** - **D) Syntax error
**
๐ก Explanationโ
Tuples are immutable.
Once a tuple is created, you cannot change (assign) values to specific indices. You would have to reassign the entire variable.
IntermediateAugmented Assignment
What is the purpose of a += 1?โ
- **A) It defines a constant
** - **B) Incrementing counters (In-Place)
** - **C) Creating a list
** - **D) Comparing 'a' to 1
**
๐ก Explanationโ
Incrementing counters.
This is "Augmented Assignment". It adds 1 to the current value of a and reassigns the result back to a.
BeginnerEvaluation Order
Which side of an assignment (=) is evaluated first?โ
- **A) The Left Side (Variable)
** - **B) The Right Side (Value)
** - **C) Both simultaneously
** - **D) It is random
**
๐ก Explanationโ
The Right Side.
Python strictly evaluates the expression on the right before assigning the result to the name on the left.
ArchitectMemory Refs
If list_a = [1, 2] and list_b = list_a, what happens if you modify list_a?โ
- **A)
list_balso changes** - **B)
list_bstays the same** - **C) Python throws an error
** - **D)
list_abecomes immutable**
๐ก Explanationโ
list_b also changes.
Since both variables are tags pointing to the same list object in memory, modifying the object via one tag is visible via the other.
AdvancedUnpacking Ignore
How do you unpack just the IP from config = ("192.168.1.1", 8080) and ignore the port?โ
- **A)
ip = config``** - **B)
ip, port = config``** - **C)
ip, _ = config``** - **D)
ip = config[0]``**
๐ก Explanationโ
ip, _ = config
By convention, the underscore _ is used as a "throwaway" variable name for values you want to unpack but don't intend to use.
๐ Quiz Complete!โ
Your Final Score:
0/15 Novice
Keep practicing to reach Architect level!
- **โบ Retake Quiz
**