🎩 Master Quiz: Assignments & Memory
Prove your “Architect” level understanding of Python’s memory model.
Green = Correct, Red = Incorrect.
In Python, how is a variable best described compared to languages like C++?
💡 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.
What does “Dynamic Typing” mean in Python?
💡 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.
If you run y = x, what actually happens in memory?
💡 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.
What is the primary use case for x = y = z = 0?
💡 Explanation
Initializing counters/defaults.
This is useful when you want multiple variables to start with the same value (pointing to the same object).
When does Python’s Garbage Collector remove an object?
💡 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.
What is the value of b in: a, b, c = "Orange", "Banana", "Cherry"?
💡 Explanation
“Banana”.
This is positional assignment. The second variable b gets the second value "Banana".
In first, *rest = [1, 2, 3, 4], what is rest?
💡 Explanation
[2, 3, 4] (List).
The * operator grabs all “remaining” items and packs them into a list.
What is the “Pythonic” way to swap two variables?
💡 Explanation
x, y = y, x
Python’s tuple unpacking allows you to swap values in a single line without needing a temporary variable.
Which function lets you check the reference count of an object?
💡 Explanation
sys.getrefcount()
This function from the sys module allows you to peek under the hood and see how many variables reference an object.
What happens if you run a, b = [1, 2, 3]?
💡 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.
Why does my_tuple[0] = 5 fail?
💡 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.
What is the purpose of a += 1?
💡 Explanation
Incrementing counters.
This is “Augmented Assignment”. It adds 1 to the current value of a and reassigns the result back to a.
Which side of an assignment (=) is evaluated first?
💡 Explanation
The Right Side.
Python strictly evaluates the expression on the right before assigning the result to the name on the left.
If list_a = [1, 2] and list_b = list_a, what happens if you modify list_a?
💡 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.
How do you unpack just the IP from config = ("192.168.1.1", 8080) and ignore the port?
💡 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.