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.
Create an Animal class with a breathe() method, then a Dog class that inherits from it and gets that method for free.
Give the child class a method of its own, and show the parent does not get it.
Use isinstance() to show an object belongs to its own class and to every ancestor.
Use issubclass() to ask about the classes themselves rather than an object.
Override a parent method in the child and show the child's version wins.
Use super() to call the parent's version of a method you are overriding.
Call super().__init__() so the parent's constructor still runs when the child adds attributes.
Show a child that passes some arguments straight through and fixes others.
Build a three-level hierarchy and show a method reaching down from the top.
Override __str__ in a child so each class prints differently.
Loop over objects of different classes calling the same method on each.
Show that polymorphism in Python needs no shared parent at all.
Use ABC and @abstractmethod to define a class that cannot be created directly.
Show that a subclass which forgets an abstract method also cannot be created.
Give an abstract class a concrete method that its children share.
Use a single leading underscore to mark an attribute as internal, and show Python does not enforce it.
Use two leading underscores and show how name mangling makes accidental access much harder.
Turn a method into a read-only attribute with @property.
Show that a property with no setter is read-only.
Add a setter so an attribute can validate itself when written.
Build a class that HAS another object rather than being one.
Build one object out of several parts.
Show that class attributes are inherited, and that a child can override one.
Print a class's method resolution order to see exactly where Python looks.
Show a child extending a parent method by calling super() first and then adding to the result.
Build a shape hierarchy with an abstract base and three concrete shapes, then report on all of them together.
Show super() passing extra arguments up a three-level chain.
Create a class that inherits from two parents and uses methods from both.
Build a diamond hierarchy and use the MRO to explain which method runs.
Use a mixin โ a small class that adds one capability to whatever it is combined with.
Define __add__ so + works on your objects.
Define __lt__ so sorted() works on your objects directly โ the Topic 15 tuple trick, retired.
Define __contains__ and __getitem__ so in and [] work.
Write your own context manager with __enter__ and __exit__ โ the promise made in Topic 13.
Show what the three arguments to __exit__ contain, and what returning True does.
Build a timing context manager that reports how long its block took.
Use a property to keep two attributes automatically in step.
Combine __private storage with a property so the rules cannot be bypassed.
Show composition letting you swap a part at runtime.
Write one function that works with any object providing the right method.
Put the shared sequence of steps in the base class and let children fill in the pieces.
Extend the custom exception hierarchy from Topic 13 with classes that carry data.
Use a class attribute that each subclass overrides to configure shared behaviour.
Show hasattr used to check for a capability rather than a type.
Show aggregation โ an object that refers to others it did not create and does not own.
Give a class a full set of comparisons and show what each enables.
Use an abstract base class to define a plugin interface, then register several plugins.
Show super() working cooperatively through multiple inheritance.
Replace an if/elif chain on type with polymorphism, and show why it is better.
Build a class hierarchy where the base does real work and children only configure it.
Build an employee hierarchy where each role calculates pay differently.
Build a bank with account types that differ in interest and overdraft rules.
Build a media library where different item types share a base but display differently.
Build a notification dispatcher that sends through whichever channels are configured.
Build a game character hierarchy with shared combat rules and per-class abilities.
Build a context manager that logs everything happening inside its block.
Build a computer from swappable parts and price the whole configuration.
Build a shopping system where item types apply different tax rules.
Build a payment system where each method validates itself differently.
Build a class where every attribute is validated through properties.
Build a context manager that guarantees a resource pool is returned even on failure.
Build a file export system where the format is chosen at runtime.
Build a sensor hierarchy with shared alert logic and per-sensor ranges.
Build a document made of sections, where each section renders itself.
Build a discount engine where rules can be combined.
Build a state machine where each state is a class deciding what comes next.
Build a zoo where feeding behaviour differs but the daily routine is shared.
Build a Vector class supporting arithmetic and comparison.
Build a class whose internal state cannot be corrupted from outside.
Build a system where the same report is produced for objects of several unrelated classes.
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)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}")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 = valueExplain why this prints what it does, and why Python sometimes refuses to build a class at all.
Show that __private is not actually private, and explain what it is really for.
Show how overriding one method can silently break another the parent relies on.
Show the difference between type(x) == C and isinstance(x, C) when inheritance is involved.
Show what happens to __hash__ when a subclass defines __eq__.
Show that an abstract class with no abstract methods can be instantiated, and why that surprises people.
Show a subclass that breaks its parent's promise, and why "is-a" was the wrong relationship.
Show the danger of __exit__ returning a truthy value by accident.
Show inheritance used where composition was the right answer, and rewrite it.
Show why super() is safer than naming the parent class directly.
Show a subclass that changes a method's signature, and why that breaks polymorphism.
Show a deep hierarchy becoming unmanageable, and flatten it with composition.
Build a Payroll System with an abstract Employee base, three roles, a department composed of employees, and a full report.
Build a Shape Toolkit with an abstract base, six shapes, sorting by area and a summary report.
Build a Banking System with account types, properties guarding the balance, and a transfer that cannot lose money.
Build a Zoo Management System combining inheritance, composition and polymorphism.
Build a Plugin Framework with an abstract base, a registry, and pipelines assembled at runtime.
Build a Resource Manager where several context managers nest to guarantee cleanup in the right order.
Build an Order Processing System where order states are classes and discounts are composed.
Build a Content Management System where content types render differently but share validation.
Build a Transport Booking System using composition for vehicles and polymorphism for pricing.
Build a Game Engine combining abstract characters, composed inventories and a context manager for each battle.
Compare inheritance and composition. Give the test for choosing, and show the same problem solved both ways.
Explain precisely what super() does, why it is not "call my parent", and when the difference matters.
Explain encapsulation in Python: what the conventions mean, what is enforced, and how @property fits.
Explain polymorphism and duck typing, how they differ from other languages, and when an abstract base class is still worth having.
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