🎩 Master Quiz: Mutable vs Immutable
20 Questions. Understanding the “Certificate vs Google Doc” analogy.
Green = Correct, Red = Incorrect.
In the “Certificate vs Google Doc” analogy, what is a String?
💡 Explanation
A Printed Certificate.
If you want to change a string, you can’t edit it. You must print a brand new one (create a new object).
If x = 10 (ID: 1001), what happens to the ID after x = x + 1?
💡 Explanation
ID changes.
Integers are immutable. Adding 1 creates a totally new object in memory, and x is moved to point to it.
If lst = [1, 2] (ID: 5001), what happens to the ID after lst.append(3)?
💡 Explanation
ID stays 5001.
Lists are mutable (Google Docs). You are updating the existing object in place without creating a new one.
Which of the following is an Immutable type?
💡 Explanation
Tuple.
Lists, Sets, and Dictionaries are all Mutable. Tuples, Strings, and Numbers are Immutable.
Why can’t you use a List as a Dictionary Key?
💡 Explanation
Lists are Mutable (Unhashable).
If the key changes content, its “Hash” changes, and Python would lose track of the value. Keys must be Immutable.
Which type is inherently Thread-Safe for sharing configuration?
💡 Explanation
Immutable types.
Since they cannot be modified, multiple threads can read them simultaneously without needing locks.
What is the result of int(3.99)?
💡 Explanation
3 (Data Loss via Truncation).
Casting a float to an int in Python always cuts off the decimal part; it does not round.
What is the result of bool("False")?
💡 Explanation
True.
Any non-empty string is “Truthier” in Python. Only an empty string "" evaluates to False.
If you need a Set to be a Dictionary Key, what must you use?
💡 Explanation
frozenset().
Normal sets are mutable and unhashable. A frozenset is immutable and can be used as a key.
Which of these values implicitly evaluates to False?
💡 Explanation
[] (Empty List).
Empty collections (lists, tuples, dicts) and 0 are False. Non-empty collections and non-zero numbers are True.
What is the Pythonic way to check if a list is not empty?
💡 Explanation
if my_list:
This leverages Implicit Boolean Conversion. If the list has items, it is True. If empty, it is False.
Why is s += " word" bad inside a massive loop?
💡 Explanation
It creates a new object every iteration.
Since strings are immutable, Python has to create a new, larger string in memory for every single addition, killing performance.
What is the optimized solution for joining many strings?
💡 Explanation
Collect in a list, then join.
Appends to a list are cheap (O(1)). Joining them at the end happens in one go.
Can a Tuple change if it contains a List? (e.g., t = (1, [a]))
💡 Explanation
Yes (The Tuple Loophole).
The tuple protects the reference to the list, but it does not protect the contents of the list itself.
For immutable objects (like Strings), how do you “change” them?
💡 Explanation
Reassignment (Rebinding).
You aren’t changing the old object; you are creating a new one and moving the variable tag to point to it.
When should you prefer Mutable types (Lists)?
💡 Explanation
Data buffers and dynamic logs.
When you need to frequently add, remove, or change items efficiently, use a Mutable type.
When should you prefer Immutable types (Tuples)?
💡 Explanation
Ensuring data integrity.
If you have data that should not change (like server coordinates), use a Tuple to prevent accidental modification.
What does tuple([1, 2]) return?
💡 Explanation
(1, 2).
It converts the mutable list into an immutable tuple.
What does “Dynamic Typing” imply about variables?
💡 Explanation
A variable is just a label.
x can point to an Integer now, and later be reassigned to point to a String. The type belongs to the object, not the name.
Is a standard set Mutable or Immutable?
💡 Explanation
Mutable.
You can add and remove items from a standard set ({1, 2}). That is why you cannot use a set as a dictionary key (unless it’s a frozenset).