Object-oriented Python · 10 sample answers

Python classes: practice questions with answers.

Classes become useful when data and the operations on that data belong together. The first hurdles are mechanical: what self refers to, where attributes are created, and why one shared class attribute can surprise every instance.

44 questions in the full bank · 19 choice · 10 fill · 15 output

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

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq1063

    1. In class MilkTea, what does the self parameter represent?

    1. the class itself
    2. the current instance object (this cup)
    3. the parent class
    4. the module
    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. Multiple choiceq1069

    2. After a class defines __len__, what can its objects do?

    1. use len() directly
    2. participate in addition directly
    3. become lists automatically
    4. avoid garbage collection
    Show answer

    Answer

    A. use len() directly

    Why: 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. Multiple choiceq1075

    3. 🧋 The shop name is shared while flavors are independent. What characterizes a class variable?

    1. all instances share it
    2. each instance has its own
    3. it cannot be modified
    4. it is visible only inside the class
    Show answer

    Answer

    A. all instances share it

    Why: 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. Multiple choiceq1086

    4. Which method runs first when an instance is created?

    1. __new__
    2. __init__
    3. __str__
    4. __del__
    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. Fill in the blankq1065

    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. Fill in the blankq1080

    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. Fill in the blankq1089

    7. Which decorator automatically generates __init__ and __repr__?

    Show answer

    Answer

    dataclass

    Why: `@dataclass` generates `__init__`, `__repr__` and `__eq__` from the annotated fields, which is why it is applied above the class statement.

  8. Output predictionq1064

    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 L

    Why: `c.size = "L"` writes to the instance dictionary, leaving `Cup.size` untouched. The class attribute still supplies the default for every other instance.

  9. Output predictionq1090

    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. Output predictionq1095

    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.

Open full practice