Python Programming · High School (11–12) · 1 course, 7 worked problems, 53 practice questions
Inheritance lets a new class reuse an old one. A class Dog(Animal) gets all the attributes and methods of Animal, and it can add new ones or override old ones. The function super() calls the parent's version of a method. Special methods have names that start and end with two underscores, and they let your objects work with Python's own syntax. __str__ sets what print() shows, __add__ lets you use +, and __eq__ decides when two objects are equal. With these, a Vector class can add and print vectors like numbers.
super() to call a method of the parent class__str__ and __add__ methods to a classChoose an answer, then check it. Each one shows a full explanation.
A subclass adds a method but no constructor of its own. class Item: def __init__(self, name, price): self.name = name self.price = price class Book(Item): def label(self): return self.name.upper() b = Book("atlas", 300) print(b.label(), b.price) c = Book("map") What happens when the program runs?
A class inherits from two parents. Both parents have a constructor and a method called start.
class Engine:
def __init__(self):
self.power = 100
def start(self):
return "engine"
class Radio:
def __init__(self):
self.volume = 5
def start(self):
return "radio"
class Car(Engine, Radio):
pass
c = Car()
print(c.start(), hasattr(c, "power"), hasattr(c, "volume"))
What does the program print?
The constructor of a parent class calls a method that the child class overrides. class Widget: def __init__(self): self.parts = [] self.setup() def setup(self): self.parts.append("base") class Button(Widget): def __init__(self, text): super().__init__() self.text = text def setup(self): self.parts.append(self.text) b = Button("OK") print(b.parts) What happens when the program runs?
Three constructors in a chain each set or change the same attribute. class A: def __init__(self): self.trail = ["A"] class B(A): def __init__(self): self.trail = ["B"] super().__init__() class C(B): def __init__(self): super().__init__() self.trail.append("C") print(C().trail) What does the program print?
A subclass inherits __str__ from its parent, and a third class extends it with super().
class Animal:
sound = "..."
def __str__(self):
return f"{type(self).__name__} says {self.sound}"
class Cat(Animal):
sound = "meow"
class Kitten(Cat):
def __str__(self):
return "small " + super().__str__()
print(Cat(), Kitten(), sep=" | ")
What does the program print?
A base class has a method that only its subclasses should fill in. It raises NotImplementedError until they do.
class Shape:
def area(self):
raise NotImplementedError
def describe(self):
return f"area {self.area()}"
class Square(Shape):
def __init__(self, s):
self.s = s
def area(self):
return self.s * self.s
class Blob(Shape):
pass
Each line below is run on its own, after these classes. Which of them run without raising an exception? Select all that apply.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A duck can both swim and fly, so its class lists two parents.
class Swimmer:
pass
class Flyer:
pass
class Duck(Swimmer, Flyer):
pass
Which of these facts about the three classes come out True? Select all that apply.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A VIP ticket should cost 50 more than a normal ticket, so the program should print 150. It stops with RecursionError instead.
class Ticket:
def price(self):
return 100
class Vip(Ticket):
def price(self):
return self.price() + 50
print(Vip().price())
Which one change makes it print 150?
In this program the line marked ____ is missing. The program should print Ira 30.
class Named:
def __init__(self, name):
self.name = name
class Aged:
def __init__(self, age):
self.age = age
class Person(Named, Aged):
def __init__(self, name, age):
Named.__init__(self, name)
____
p = Person("Ira", 30)
print(p.name, p.age)
Which line, put in place of ____, makes the program print Ira 30?
Three classes are written first. Then each line below is added, on its own, after them. class A: pass class B(A): pass class C(A): pass Which of these lines run without raising an exception? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
All 53 practice questions on Inheritance and Special Methods →
Each one has a hint, the key idea and a full worked solution.