Python ยท Object Oriented Programming

OOP Intermediate: Practice Questions

100 questions. Try each one yourself before checking the answer.

Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.

Q1InheritanceEasyMust Do

Create an Animal class with a breathe() method, then a Dog class that inherits from it and gets that method for free.

Q2InheritanceEasy

Give the child class a method of its own, and show the parent does not get it.

Q3isinstanceEasy

Use isinstance() to show an object belongs to its own class and to every ancestor.

Q4issubclassEasy

Use issubclass() to ask about the classes themselves rather than an object.

Q5OverridingEasyMust Do

Override a parent method in the child and show the child's version wins.

Q6super()Easy

Use super() to call the parent's version of a method you are overriding.

Q7super().__init__EasyMust Do

Call super().__init__() so the parent's constructor still runs when the child adds attributes.

Q8super().__init__Easy

Show a child that passes some arguments straight through and fixes others.

Q9Multilevel InheritanceEasy

Build a three-level hierarchy and show a method reaching down from the top.

Q10OverridingEasy

Override __str__ in a child so each class prints differently.

Q11PolymorphismEasyMust Do

Loop over objects of different classes calling the same method on each.

Q12Duck TypingEasy

Show that polymorphism in Python needs no shared parent at all.

Q13Abstract ClassesEasyMust Do

Use ABC and @abstractmethod to define a class that cannot be created directly.

Q14Abstract ClassesEasy

Show that a subclass which forgets an abstract method also cannot be created.

Q15Abstract ClassesEasy

Give an abstract class a concrete method that its children share.

Q16EncapsulationEasy

Use a single leading underscore to mark an attribute as internal, and show Python does not enforce it.

Q17EncapsulationEasyMust Do

Use two leading underscores and show how name mangling makes accidental access much harder.

Q18@propertyEasy

Turn a method into a read-only attribute with @property.

Q19@propertyEasy

Show that a property with no setter is read-only.

Q20Property SettersEasyMust Do

Add a setter so an attribute can validate itself when written.

Q21CompositionEasyMust Do

Build a class that HAS another object rather than being one.

Q22CompositionEasy

Build one object out of several parts.

Q23Class AttributesEasy

Show that class attributes are inherited, and that a child can override one.

Q24MROEasy

Print a class's method resolution order to see exactly where Python looks.

Q25OverridingEasy

Show a child extending a parent method by calling super() first and then adding to the result.

Q26Abstract ClassesMediumMust Do

Build a shape hierarchy with an abstract base and three concrete shapes, then report on all of them together.

Q27super()Medium

Show super() passing extra arguments up a three-level chain.

Q28Multiple InheritanceMedium

Create a class that inherits from two parents and uses methods from both.

Q29MROMediumMust Do

Build a diamond hierarchy and use the MRO to explain which method runs.

Q30MixinsMedium

Use a mixin โ€” a small class that adds one capability to whatever it is combined with.

Q31Operator OverloadingMedium

Define __add__ so + works on your objects.

Q32Operator OverloadingMediumMust Do

Define __lt__ so sorted() works on your objects directly โ€” the Topic 15 tuple trick, retired.

Q33Operator OverloadingMedium

Define __contains__ and __getitem__ so in and [] work.

Q34Context ManagersMediumMust Do

Write your own context manager with __enter__ and __exit__ โ€” the promise made in Topic 13.

Q35Context ManagersMedium

Show what the three arguments to __exit__ contain, and what returning True does.

Q36Context ManagersMedium

Build a timing context manager that reports how long its block took.

Q37@propertyMedium

Use a property to keep two attributes automatically in step.

Q38EncapsulationMedium

Combine __private storage with a property so the rules cannot be bypassed.

Q39CompositionMediumMust Do

Show composition letting you swap a part at runtime.

Q40PolymorphismMedium

Write one function that works with any object providing the right method.

Q41Template MethodMedium

Put the shared sequence of steps in the base class and let children fill in the pieces.

Q42InheritanceMedium

Extend the custom exception hierarchy from Topic 13 with classes that carry data.

Q43Class AttributesMedium

Use a class attribute that each subclass overrides to configure shared behaviour.

Q44Duck TypingMedium

Show hasattr used to check for a capability rather than a type.

Q45CompositionMedium

Show aggregation โ€” an object that refers to others it did not create and does not own.

Q46Operator OverloadingMedium

Give a class a full set of comparisons and show what each enables.

Q47Abstract ClassesMedium

Use an abstract base class to define a plugin interface, then register several plugins.

Q48super()Medium

Show super() working cooperatively through multiple inheritance.

Q49PolymorphismMediumMust Do

Replace an if/elif chain on type with polymorphism, and show why it is better.

Q50InheritanceMedium

Build a class hierarchy where the base does real work and children only configure it.

Q51Real-WorldMediumMust Do

Build an employee hierarchy where each role calculates pay differently.

Q52Real-WorldMedium

Build a bank with account types that differ in interest and overdraft rules.

Q53Real-WorldMedium

Build a media library where different item types share a base but display differently.

Q54Real-WorldMediumMust Do

Build a notification dispatcher that sends through whichever channels are configured.

Q55Real-WorldMedium

Build a game character hierarchy with shared combat rules and per-class abilities.

Q56Context ManagersMedium

Build a context manager that logs everything happening inside its block.

Q57CompositionMediumMust Do

Build a computer from swappable parts and price the whole configuration.

Q58Real-WorldMedium

Build a shopping system where item types apply different tax rules.

Q59Real-WorldMedium

Build a payment system where each method validates itself differently.

Q60EncapsulationMedium

Build a class where every attribute is validated through properties.

Q61Context ManagersMediumMust Do

Build a context manager that guarantees a resource pool is returned even on failure.

Q62PolymorphismMedium

Build a file export system where the format is chosen at runtime.

Q63Real-WorldMedium

Build a sensor hierarchy with shared alert logic and per-sensor ranges.

Q64CompositionMedium

Build a document made of sections, where each section renders itself.

Q65Real-WorldMedium

Build a discount engine where rules can be combined.

Q66Abstract ClassesMedium

Build a state machine where each state is a class deciding what comes next.

Q67Real-WorldMedium

Build a zoo where feeding behaviour differs but the daily routine is shared.

Q68Operator OverloadingMedium

Build a Vector class supporting arithmetic and comparison.

Q69EncapsulationMediumMust Do

Build a class whose internal state cannot be corrupted from outside.

Q70PolymorphismMedium

Build a system where the same report is produced for objects of several unrelated classes.

Q71super()HardMust Do

This crashes with AttributeError. Explain what the child forgot.

class Animal:
    def __init__(self, name):
        self.name = name
 
class Dog(Animal):
    def __init__(self, name, breed):
        self.breed = breed
 
rex = Dog("Rex", "Labrador")
print(rex.name)
Q72Class AttributesHard

Both subclasses end up sharing one list. Explain and fix.

class Container:
    items = []
 
    def add(self, item):
        self.items.append(item)
 
class Box(Container):
    pass
 
class Bag(Container):
    pass
 
box = Box()
bag = Bag()
box.add("book")
bag.add("apple")
 
print(f"box: {box.items}")
print(f"bag: {bag.items}")
Q73@propertyHardMust Do

This setter causes RecursionError. Explain and fix.

class Person:
    def __init__(self, age):
        self.age = age
 
    @property
    def age(self):
        return self.age
 
    @age.setter
    def age(self, value):
        self.age = value
Q74MROHard

Explain why this prints what it does, and why Python sometimes refuses to build a class at all.

Q75EncapsulationHard

Show that __private is not actually private, and explain what it is really for.

Q76Fragile Base ClassHard

Show how overriding one method can silently break another the parent relies on.

Q77isinstanceHard

Show the difference between type(x) == C and isinstance(x, C) when inheritance is involved.

Q78__eq__Hard

Show what happens to __hash__ when a subclass defines __eq__.

Q79Abstract ClassesHard

Show that an abstract class with no abstract methods can be instantiated, and why that surprises people.

Q80LiskovHard

Show a subclass that breaks its parent's promise, and why "is-a" was the wrong relationship.

Q81Context ManagersHard

Show the danger of __exit__ returning a truthy value by accident.

Q82CompositionHardMust Do

Show inheritance used where composition was the right answer, and rewrite it.

Q83super()Hard

Show why super() is safer than naming the parent class directly.

Q84PolymorphismHard

Show a subclass that changes a method's signature, and why that breaks polymorphism.

Q85DesignHard

Show a deep hierarchy becoming unmanageable, and flatten it with composition.

Q86Mini-ProjectMini-Project

Build a Payroll System with an abstract Employee base, three roles, a department composed of employees, and a full report.

Q87Mini-ProjectMini-Project

Build a Shape Toolkit with an abstract base, six shapes, sorting by area and a summary report.

Q88Mini-ProjectMini-ProjectMust Do

Build a Banking System with account types, properties guarding the balance, and a transfer that cannot lose money.

Q89Mini-ProjectMini-Project

Build a Zoo Management System combining inheritance, composition and polymorphism.

Q90Mini-ProjectMini-ProjectMust Do

Build a Plugin Framework with an abstract base, a registry, and pipelines assembled at runtime.

Q91Mini-ProjectMini-Project

Build a Resource Manager where several context managers nest to guarantee cleanup in the right order.

Q92Mini-ProjectMini-Project

Build an Order Processing System where order states are classes and discounts are composed.

Q93Mini-ProjectMini-Project

Build a Content Management System where content types render differently but share validation.

Q94Mini-ProjectMini-Project

Build a Transport Booking System using composition for vehicles and polymorphism for pricing.

Q95Mini-ProjectMini-Project

Build a Game Engine combining abstract characters, composed inventories and a context manager for each battle.

Q96InterviewInterview

Compare inheritance and composition. Give the test for choosing, and show the same problem solved both ways.

Q97super()Interview

Explain precisely what super() does, why it is not "call my parent", and when the difference matters.

Q98EncapsulationInterview

Explain encapsulation in Python: what the conventions mean, what is enforced, and how @property fits.

Q99PolymorphismInterview

Explain polymorphism and duck typing, how they differ from other languages, and when an abstract base class is still worth having.

Q100CapstoneInterviewMust Do

Capstone. Build an OOP Toolkit Report demonstrating every relationship in this topic: an abstract base, inheritance with super(), polymorphism across subclasses, duck typing across unrelated classes, composition, encapsulation with properties, operator overloading, a mixin, and a context manager.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session