0
0
Pythonprogramming~20 mins

OOP principles overview in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OOP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using inheritance?
Consider this Python code that uses inheritance. What will it print when run?
Python
class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def sound(self):
        return "Bark"

pet = Dog()
print(pet.sound())
ATypeError
BSome sound
CNone
DBark
Attempts:
2 left
💡 Hint
Think about which method is called when the child class overrides a method from the parent.
Predict Output
intermediate
2:00remaining
What does this code print demonstrating encapsulation?
Look at this Python class using encapsulation. What will be printed?
Python
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance
    def get_balance(self):
        return self.__balance

account = BankAccount(100)
print(account.__balance)
ANone
B100
CAttributeError
D0
Attempts:
2 left
💡 Hint
Check how private variables are accessed outside the class.
🧠 Conceptual
advanced
2:00remaining
Which principle does this code best demonstrate?
This code shows a class with a method that behaves differently depending on the object. Which OOP principle is this?
Python
class Shape:
    def area(self):
        return 0

class Square(Shape):
    def __init__(self, side):
        self.side = side
    def area(self):
        return self.side * self.side

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    def area(self):
        return 3.14 * self.radius * self.radius
APolymorphism
BInheritance
CEncapsulation
DAbstraction
Attempts:
2 left
💡 Hint
Think about how the same method name works differently for different classes.
Predict Output
advanced
2:00remaining
What is the output of this code demonstrating abstraction?
What will this code print when run?
Python
from abc import ABC, abstractmethod

class Vehicle(ABC):
    @abstractmethod
    def start(self):
        pass

class Car(Vehicle):
    def start(self):
        return "Car started"

my_car = Car()
print(my_car.start())
ACar started
BTypeError
CAttributeError
DVehicle started
Attempts:
2 left
💡 Hint
Look at how abstract methods are implemented in child classes.
Predict Output
expert
2:00remaining
What is the value of x after running this code with multiple inheritance?
Consider this code with multiple inheritance. What is the value of x after execution?
Python
class A:
    def __init__(self):
        self.x = 1

class B(A):
    def __init__(self):
        super().__init__()
        self.x = 2

class C(A):
    def __init__(self):
        super().__init__()
        self.x = 3

class D(B, C):
    def __init__(self):
        super().__init__()

obj = D()
x = obj.x
A1
B2
C3
DTypeError
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for class D.