Bird
Raised Fist0
Pythonprogramming~20 mins

OOP principles overview in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
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.

Practice

(1/5)
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
A. Compilation
B. Inheritance
C. Polymorphism
D. Encapsulation

Solution

  1. Step 1: Recall the four main OOP principles

    The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.
  2. Step 2: Identify the option not in the list

    Compilation is a process related to converting code, not an OOP principle.
  3. Final Answer:

    Compilation -> Option A
  4. Quick Check:

    OOP principles exclude Compilation [OK]
Hint: Remember OOP principles: E, I, P, A [OK]
Common Mistakes:
  • Confusing compilation with OOP concepts
  • Mixing up abstraction with compilation
  • Thinking all programming terms are OOP principles
2. Which Python keyword is used to create a new class?
easy
A. def
B. func
C. object
D. class

Solution

  1. Step 1: Recall Python syntax for defining classes

    In Python, the keyword class is used to define a new class.
  2. Step 2: Check other options

    def defines functions, object is a base class, and func is not a Python keyword.
  3. Final Answer:

    class -> Option D
  4. Quick Check:

    Use 'class' to define classes [OK]
Hint: Classes start with 'class' keyword in Python [OK]
Common Mistakes:
  • Using def instead of class for classes
  • Confusing object with class keyword
  • Trying to use func which is invalid
3. What will be the output of this code?
class Animal:
    def speak(self):
        return "Sound"

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

pet = Dog()
print(pet.speak())
medium
A. Bark
B. Sound
C. None
D. Error

Solution

  1. Step 1: Understand inheritance and method overriding

    Dog class inherits from Animal and overrides the speak method to return "Bark".
  2. Step 2: Check which speak method is called

    pet is an instance of Dog, so pet.speak() calls Dog's speak method, returning "Bark".
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Overridden method returns 'Bark' [OK]
Hint: Child class method overrides parent method [OK]
Common Mistakes:
  • Expecting parent class method output
  • Confusing method overriding with overloading
  • Thinking print outputs None
4. Find the error in this code snippet:
class Car:
    def __init__(self, model):
        self.model = model

    def display(self):
        print(Model)
medium
A. Constructor name is wrong
B. Missing self in display method
C. Model should be self.model in print
D. Class name should be lowercase

Solution

  1. Step 1: Check the print statement inside display method

    The print statement uses Model which is undefined; it should use self.model to access the instance variable.
  2. Step 2: Verify other parts

    The constructor name __init__ is correct, and method has self parameter. Class name capitalization is fine.
  3. Final Answer:

    Model should be self.model in print -> Option C
  4. Quick Check:

    Use self to access instance variables [OK]
Hint: Use self.variable to access instance data [OK]
Common Mistakes:
  • Forgetting self in method parameters
  • Using variable name without self prefix
  • Thinking constructor name is incorrect
5. You want to create a class that hides its internal data and only allows access through methods. Which OOP principle does this demonstrate?
hard
A. Inheritance
B. Encapsulation
C. Polymorphism
D. Abstraction

Solution

  1. Step 1: Understand the principle of hiding data

    Hiding internal data and controlling access through methods is called Encapsulation.
  2. Step 2: Differentiate from other principles

    Inheritance is about reusing code, Polymorphism is about using methods in different ways, Abstraction is about hiding complexity but not necessarily data.
  3. Final Answer:

    Encapsulation -> Option B
  4. Quick Check:

    Data hiding = Encapsulation [OK]
Hint: Data hiding means Encapsulation [OK]
Common Mistakes:
  • Confusing encapsulation with abstraction
  • Mixing inheritance with data hiding
  • Thinking polymorphism hides data