Object-Oriented Programming Basics Flashcards
7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Object-Oriented Programming Basics flashcards as text
What is the Method Resolution Order (MRO) in Python?
Answer: The order Python searches classes to find a method when multiple inheritance is used
MRO defines the hierarchy Python follows (using C3 linearization) to look up methods and attributes across a class inheritance chain.
Which dunder method allows objects to be compared with == ?
Answer: __eq__
__eq__ is called when == is used; if not defined, Python defaults to identity comparison (same as 'is').
What is a mixin in Python OOP?
Answer: A class that cannot be used as a standalone object and provides reusable methods via multiple inheritance
A mixin is a class designed to provide methods to other classes through multiple inheritance without being used as a standalone base class.
What does __del__ do in Python?
Answer: Is called when an object is about to be garbage collected
__del__ is a finalizer called by the garbage collector just before the object is destroyed; it is not a destructor in the C++ sense.
How can you check if an object is an instance of a given class or its subclasses?
Answer: isinstance(obj, ClassName)
isinstance(obj, ClassName) returns True if obj is an instance of ClassName or any subclass of it, unlike type() which only checks exact type.
What is the difference between issubclass() and isinstance()?
Answer: issubclass() checks class-to-class relationship; isinstance() checks object-to-class relationship
issubclass(Child, Parent) checks if a class inherits from another; isinstance(obj, Class) checks if an object is an instance of a class (or subclass).
What will this code output? class Counter: count = 0 def __init__(self): Counter.count += 1 a = Counter() b = Counter() print(Counter.count)
Answer: 2
Each instantiation increments the class attribute Counter.count; after two objects are created, it equals 2.