Python Mutable vs. Immutable Types Quiz
Progress
Score: 0/20
๐ฉ Master Quiz: Mutable vs Immutable
20 Questions. Understanding the "Certificate vs Google Doc" analogy. Green = Correct, Red = Incorrect.
BeginnerAnalogy
In the "Certificate vs Google Doc" analogy, what is a String?โ
- **A) A Google Doc (Mutable)
** - **B) A Printed Certificate (Immutable)
** - **C) A Sticky Note
** - **D) A Whiteboard
**
๐ก 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).
IntermediateMemory ID
If x = 10 (ID: 1001), what happens to the ID after x = x + 1?โ
- **A) ID stays 1001 (Modified in place)
** - **B) ID becomes None
** - **C) ID changes (e.g., 2005)
** - **D) ID increments by 1 (1002)
**
๐ก Explanationโ
ID changes.
Integers are immutable. Adding 1 creates a totally new object in memory, and x is moved to point to it.
IntermediateMemory ID
If lst = [1, 2] (ID: 5001), what happens to the ID after lst.append(3)?โ
- **A) ID stays 5001 (Same address)
** - **B) ID changes completely
** - **C) It throws an error
** - **D) ID becomes 5002
**
๐ก Explanationโ
ID stays 5001.
Lists are mutable (Google Docs). You are updating the existing object in place without creating a new one.
BeginnerClassification
Which of the following is an Immutable type?โ
- **A) List
** - **B) Tuple
** - **C) Set
** - **D) Dictionary
**
๐ก Explanationโ
Tuple.
Lists, Sets, and Dictionaries are all Mutable. Tuples, Strings, and Numbers are Immutable.
ArchitectDict Keys
Why can't you use a List as a Dictionary Key?โ
- **A) Lists are too large
** - **B) Lists are not unique
** - **C) Lists are Mutable (Unhashable)
** - **D) Python only allows Strings as keys
**
๐ก 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.
ArchitectThread Safety
Which type is inherently Thread-Safe for sharing configuration?โ
- **A) Immutable types (e.g., Tuple)
** - **B) Mutable types (e.g., List)
** - **C) Global variables
** - **D) None of them
**
๐ก Explanationโ
Immutable types.
Since they cannot be modified, multiple threads can read them simultaneously without needing locks.
IntermediateType Casting
What is the result of int(3.99)?โ
- **A) 4 (Rounded)
** - **B) 3 (Truncated)
** - **C) 3.99
** - **D) TypeError
**
๐ก Explanationโ
3 (Data Loss via Truncation).
Casting a float to an int in Python always cuts off the decimal part; it does not round.
IntermediateLogic Error
What is the result of bool("False")?โ
- **A) True
** - **B) False
** - **C) Error
** - **D) None
**
๐ก Explanationโ
True.
Any non-empty string is "Truthier" in Python. Only an empty string "" evaluates to False.
AdvancedFrozen Set
If you need a Set to be a Dictionary Key, what must you use?โ
- **A) set()
** - **B) list()
** - **C) frozenset()
** - **D) tupleset()
**
๐ก Explanationโ
frozenset().
Normal sets are mutable and unhashable. A frozenset is immutable and can be used as a key.
IntermediateImplicit Bool
Which of these values implicitly evaluates to False?โ
- **A) "0"
** - **B) [] (Empty List)
** - **C) [0]
** - **D) -1
**
๐ก Explanationโ
[] (Empty List).
Empty collections (lists, tuples, dicts) and 0 are False. Non-empty collections and non-zero numbers are True.
ArchitectPythonic Style
What is the Pythonic way to check if a list is not empty?โ
- **A)
if my_list:``** - **B)
if len(my_list) > 0:``** - **C)
if my_list != []:``** - **D)
if my_list is not None:``**
๐ก Explanationโ
if my_list:
This leverages Implicit Boolean Conversion. If the list has items, it is True. If empty, it is False.
AdvancedPerformance
Why is s += " word" bad inside a massive loop?โ
- **A) It overflows the stack
** - **B) It creates a new string object every iteration
** - **C) Strings are mutable
** - **D) It converts the string to a list
**
๐ก 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.
ArchitectOptimization
What is the optimized solution for joining many strings?โ
- **A) Use Tuple concatenation
** - **B) Use the Global keyword
** - **C) Use a list, then
''.join(list)``** - **D) Pre-allocate the string
**
๐ก Explanationโ
Collect in a list, then join.
Appends to a list are cheap (O(1)). Joining them at the end happens in one go.
AdvancedThe Loophole
Can a Tuple change if it contains a List? (e.g., t = (1, [a]))โ
- **A) Yes, the inner list can be modified
** - **B) No, Tuples are strictly immutable
** - **C) Only if you cast it to a list first
** - **D) Only in Python 2
**
๐ก Explanationโ
Yes (The Tuple Loophole).
The tuple protects the reference to the list, but it does not protect the contents of the list itself.
BeginnerReassignment
For immutable objects (like Strings), how do you "change" them?โ
- **A)
s.append("new")``** - **B) Reassignment (e.g.,
s = s + "new")** - **C)
s.update("new")``** - **D) You cannot change them at all
**
๐ก 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.
IntermediateUse Case
When should you prefer Mutable types (Lists)?โ
- **A) For database credentials
** - **B) For dictionary keys
** - **C) For data buffers and dynamic logs
** - **D) For constants
**
๐ก Explanationโ
Data buffers and dynamic logs.
When you need to frequently add, remove, or change items efficiently, use a Mutable type.
IntermediateUse Case
When should you prefer Immutable types (Tuples)?โ
- **A) For ensures data integrity (e.g., config)
** - **B) For collecting user inputs
** - **C) For queues
** - **D) For stacks
**
๐ก Explanationโ
Ensuring data integrity.
If you have data that should not change (like server coordinates), use a Tuple to prevent accidental modification.
BeginnerCasting
What does tuple([1, 2]) return?โ
- **A)
[1, 2]``** - **B)
(1, 2)``** - **C)
{1, 2}``** - **D)
1, 2(String)**
๐ก Explanationโ
(1, 2).
It converts the mutable list into an immutable tuple.
ArchitectDynamic Typing
What does "Dynamic Typing" imply about variables?โ
- **A) A variable is just a label; it can point to any type
** - **B) Variables are fixed to the type they start with
** - **C) You must compile code to check types
** - **D) Python only uses Strings
**
๐ก 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.
IntermediateSet
Is a standard set Mutable or Immutable?โ
- **A) Mutable
** - **B) Immutable
** - **C) It depends on the contents
** - **D) Sets don't exist in Python
**
๐ก 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).
๐ Quiz Complete!โ
Your Final Score:
0/20 Novice
Keep practicing to reach Architect level!
- **โบ Retake Quiz
**