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 Encapsulation in OOP?
Encapsulation means keeping data (attributes) and code (methods) together in one unit called a class. It hides the internal details and only exposes what is necessary.
Click to reveal answer
beginner
Define Inheritance in simple terms.
Inheritance lets a new class use properties and methods from an existing class. It helps reuse code and create a hierarchy.
Click to reveal answer
intermediate
What does Polymorphism mean in OOP?
Polymorphism means many forms. It allows different classes to use the same method name but behave differently depending on the class.
Click to reveal answer
intermediate
Explain Abstraction with an example.
Abstraction hides complex details and shows only the important parts. For example, when you drive a car, you don’t need to know how the engine works, just how to use the steering and pedals.
Click to reveal answer
beginner
Why are OOP principles useful in programming?
They help organize code better, make it reusable, easier to maintain, and model real-world things clearly.
Click to reveal answer
Which OOP principle means hiding internal details and showing only necessary parts?
AAbstraction
BInheritance
CPolymorphism
DEncapsulation
✗ Incorrect
Abstraction hides complex details and shows only what is needed.
What does inheritance allow a class to do?
AHide data from other classes
BUse methods and properties from another class
CChange method names dynamically
DCreate many forms of the same method
✗ Incorrect
Inheritance lets a class reuse code from a parent class.
Polymorphism allows methods to:
AHide data inside a class
BOnly show important details
CCreate new classes from old ones
DHave the same name but behave differently
✗ Incorrect
Polymorphism means many forms of the same method name.
Encapsulation is best described as:
AUsing many classes
BChanging method behavior
CKeeping data and methods together inside a class
DHiding complex details
✗ Incorrect
Encapsulation bundles data and methods in one unit.
Which principle helps make code easier to maintain and reuse?
AAll of the above
BPolymorphism
CInheritance
DAbstraction
✗ Incorrect
All OOP principles help improve code organization and reuse.
Describe the four main OOP principles and give a simple example for each.
Think about how a car works for abstraction.
You got /4 concepts.
Why is using OOP principles helpful when writing programs?
Imagine building with Lego blocks.
You got /4 concepts.
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
Step 1: Recall the four main OOP principles
The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.
Step 2: Identify the option not in the list
Compilation is a process related to converting code, not an OOP principle.
Final Answer:
Compilation -> Option A
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
Step 1: Recall Python syntax for defining classes
In Python, the keyword class is used to define a new class.
Step 2: Check other options
def defines functions, object is a base class, and func is not a Python keyword.
Final Answer:
class -> Option D
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
Step 1: Understand inheritance and method overriding
Dog class inherits from Animal and overrides the speak method to return "Bark".
Step 2: Check which speak method is called
pet is an instance of Dog, so pet.speak() calls Dog's speak method, returning "Bark".
Final Answer:
Bark -> Option A
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
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.
Step 2: Verify other parts
The constructor name __init__ is correct, and method has self parameter. Class name capitalization is fine.
Final Answer:
Model should be self.model in print -> Option C
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
Step 1: Understand the principle of hiding data
Hiding internal data and controlling access through methods is called Encapsulation.
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.