Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is inheritance in programming?
Inheritance is a way to create a new class using details from an existing class. It helps reuse code and organize programs better.
Click to reveal answer
beginner
Why do we use inheritance?
We use inheritance to avoid repeating code, to make programs easier to maintain, and to create relationships between classes that share common features.
Click to reveal answer
beginner
How does inheritance help with code reuse?
Inheritance lets a new class get all the features of an existing class, so we don’t have to write the same code again. We just add or change what is different.
Click to reveal answer
beginner
What is a 'parent' or 'base' class?
A parent or base class is the original class that passes its features to a new class through inheritance.
Click to reveal answer
beginner
What is a 'child' or 'derived' class?
A child or derived class is the new class that inherits features from the parent class and can add new features or change existing ones.
Click to reveal answer
What is the main purpose of inheritance in programming?
ATo make programs slower
BTo delete old code
CTo reuse code from an existing class
DTo write code without classes
✗ Incorrect
Inheritance helps reuse code by letting a new class use features from an existing class.
In inheritance, what do we call the class that provides features?
AChild class
BParent class
CSibling class
DHelper class
✗ Incorrect
The parent class is the one that passes its features to the child class.
Which of these is NOT a benefit of inheritance?
ACreating unrelated classes
BCode reuse
CEasier maintenance
DOrganizing code
✗ Incorrect
Inheritance is used to relate classes, not to create unrelated ones.
What can a child class do with features inherited from a parent class?
AAdd new features or change existing ones
BDelete the parent class
COnly use them as they are
DIgnore the parent class completely
✗ Incorrect
A child class can add new features or modify inherited ones.
Inheritance helps to:
AAvoid using classes
BMake code harder to understand
CWrite the same code multiple times
DReuse and organize code better
✗ Incorrect
Inheritance helps reuse code and organize it in a clear way.
Explain in your own words why inheritance is useful in programming.
Think about how inheritance saves time and effort when writing code.
You got /3 concepts.
Describe the difference between a parent class and a child class.
Consider who gives and who receives features.
You got /2 concepts.
Practice
(1/5)
1. What is the main purpose of inheritance in Python?
easy
A. To delete objects from memory
B. To allow a class to reuse code from another class
C. To make a program run faster
D. To create variables inside a function
Solution
Step 1: Understand inheritance concept
Inheritance lets one class (child) use code from another class (parent).
Step 2: Identify main benefit
This helps reuse code and avoid rewriting the same features.
Final Answer:
To allow a class to reuse code from another class -> Option B
Quick Check:
Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
Confusing inheritance with variable creation
Thinking inheritance speeds up program execution
Believing inheritance deletes objects
2. Which of the following is the correct syntax to define a child class Dog that inherits from a parent class Animal?
easy
A. class Dog : Animal
B. class Dog inherits Animal:
C. class Dog -> Animal:
D. class Dog(Animal):
Solution
Step 1: Recall Python inheritance syntax
In Python, a child class inherits by putting the parent class name in parentheses after the child class name.
Step 2: Match syntax options
Only class Dog(Animal): follows this rule correctly.
Final Answer:
class Dog(Animal): -> Option D
Quick Check:
Child class syntax = class Child(Parent): [OK]
Hint: Use parentheses with parent class name after child class [OK]
Common Mistakes:
Using 'inherits' keyword which doesn't exist in Python
Using arrow '->' instead of parentheses
Using colon ':' incorrectly after class name
3. What will be the output of this code?
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
pet = Dog()
print(pet.sound())
medium
A. Some sound
B. None
C. Bark
D. Error
Solution
Step 1: Understand method overriding in inheritance
The Dog class has its own sound method that replaces the one from Animal.
Step 2: Check which method is called
When pet.sound() runs, it uses the Dog version returning "Bark".
Final Answer:
Bark -> Option C
Quick Check:
Child method overrides parent method = Bark [OK]
Hint: Child method replaces parent method if same name used [OK]
Common Mistakes:
Expecting parent method output instead of child
Thinking both methods run together
Assuming error due to method name clash
4. Find the error in this inheritance code:
class Vehicle:
def move(self):
print("Moving")
class Car(Vehicle)
def move(self):
print("Car moving")
medium
A. Missing colon after class Car(Vehicle)
B. Wrong parent class name
C. Indentation error in move method
D. No error
Solution
Step 1: Check class definition syntax
Python requires a colon ':' at the end of class header lines.
Step 2: Identify missing colon
The line class Car(Vehicle) misses the colon at the end.
Final Answer:
Missing colon after class Car(Vehicle) -> Option A
Quick Check:
Class header must end with ':' [OK]
Hint: Always put ':' after class and function headers [OK]
Common Mistakes:
Forgetting colon after class definition
Assuming wrong parent class name causes error
Confusing indentation errors with syntax errors
5. You want to create a class SmartPhone that inherits features from both Phone and Camera classes. What is the correct way to define SmartPhone to reuse code from both parents?
hard
A. class SmartPhone(Phone, Camera):
B. class SmartPhone inherits Phone and Camera:
C. class SmartPhone(Phone): Camera
D. class SmartPhone : Phone, Camera
Solution
Step 1: Understand multiple inheritance syntax
Python allows a class to inherit from multiple parents by listing them in parentheses separated by commas.
Step 2: Match correct syntax
Only class SmartPhone(Phone, Camera): correctly shows multiple inheritance.