What this topic tests
- Trace __init__, self, and instance attributes through a call.
- Separate class-level state from per-instance state.
- Read inheritance and method lookup without guessing.
Common mistakes
- Forgetting self in a method signature or when calling another method.
- Putting a mutable value at class level when every instance should have its own copy.
- Confusing a class attribute with an instance attribute that shadows it.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. In class MilkTea, what does the self parameter represent?
Show answer
Answer
B. the current instance object (this cup)Why: self is the instance the method was called on, not the class. Python passes it automatically, so `c.describe()` fills self with `c` before the body runs.
-
2. After a class defines __len__, what can its objects do?
Show answer
Answer
A. use len() directlyWhy: Defining `__len__` makes `len(obj)` call it, so the object can also be used where a sized container is expected. `__str__` and `__add__` work the same way.
-
3. 🧋 The shop name is shared while flavors are independent. What characterizes a class variable?
Show answer
Answer
A. all instances share itWhy: A class variable is stored once on the class, so every instance reads the same value until one of them assigns to that name and creates an instance attribute.
-
4. Which method runs first when an instance is created?
Show answer
Answer
A. __new__Why: __new__ allocates and returns the instance; __init__ then initializes that instance. Overriding __new__ is rare outside immutable types.
-
5. What is the initialization method automatically called when creating a milk tea?
Show answer
Answer
__init__Why: `__init__` is looked up on the class and called by the constructor machinery right after the new instance exists, which is why it receives self.
-
6. Which attribute exposes an instance's attribute dictionary?
print(Cup().__dict__)Show answer
Answer
__dict__Why: __dict__ is the per-instance namespace where self.x assignments land. Class attributes live on the class, not in this dictionary.
-
7. Which decorator automatically generates __init__ and __repr__?
Show answer
Answer
dataclassWhy: `@dataclass` generates `__init__`, `__repr__` and `__eq__` from the annotated fields, which is why it is applied above the class statement.
-
8. Does assigning an instance attribute with the same name affect the class attribute?
class Cup: size = "M" c = Cup() c.size = "L" print(Cup.size, c.size)Show answer
Answer
M LWhy: `c.size = "L"` writes to the instance dictionary, leaving `Cup.size` untouched. The class attribute still supplies the default for every other instance.
-
9. What does `append([], [])` return?
def append(list1, list2): return concat([list1, list2]) def concat(lists): return [element for list in lists for element in list] def filter(function, list): return [item for item in list if function(item)] def length(list): return sum(1 for _ in list) def map(function, list): return [function(element) for element in list] def foldl(function, list, initial): if len(list) == 0: return initial else: return foldl(function, list[1:], function(initial, list[0])) def foldr(function, list, initial): if len(list) == 0: return initial else: return function(foldr(function, list[1:], initial), list[0]) def reverse(list): return list[::-1] print(append([], []))Show answer
Answer
[]Why: `append` wraps both arguments in a list and hands it to `concat`, which flattens one level; two empty lists flatten to `[]`.
-
10. What does `reverse(['xyz', 4.0, 'cat', 1])` return?
def append(list1, list2): return concat([list1, list2]) def concat(lists): return [element for list in lists for element in list] def filter(function, list): return [item for item in list if function(item)] def length(list): return sum(1 for _ in list) def map(function, list): return [function(element) for element in list] def foldl(function, list, initial): if len(list) == 0: return initial else: return foldl(function, list[1:], function(initial, list[0])) def foldr(function, list, initial): if len(list) == 0: return initial else: return function(foldr(function, list[1:], initial), list[0]) def reverse(list): return list[::-1] print(reverse(['xyz', 4.0, 'cat', 1]))Show answer
Answer
[1, 'cat', 4.0, 'xyz']Why: The negative-step slice copies elements from the end backwards; `reverse` does not compare types, so the strings and the float keep their values and only their order changes.
Common questions
What is self?
self is the instance passed to an instance method. Python supplies it automatically when you call obj.method().
When should I use a class instead of a dictionary?
Use a class when behaviour belongs with the data, or when you need a clear interface and validation. A dictionary is fine for a simple record with no behaviour.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.