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
How do you define a class method (not an instance method) in Python?
Answer: Use @classmethod decorator with 'cls' as first parameter
@classmethod receives the class as the first argument (conventionally named 'cls') and can access or modify class state.
What is method overriding in Python?
Answer: A child class provides its own implementation of a method inherited from the parent
Method overriding lets a subclass redefine an inherited method, replacing the parent's behavior with its own.
What is the output of: class A: x = 10 a1 = A() A.x = 20 print(a1.x)
Answer: 20
a1.x resolves to the class attribute A.x because no instance attribute 'x' was set on a1; after A.x = 20, it reads 20.
Which statement about Python's __str__ method is correct?
Answer: It is called by str() and print() to get a human-readable string
__str__ should return a user-friendly string; Python calls it automatically when you use str(obj) or print(obj).
What does super() do in a subclass?
Answer: Returns a proxy that delegates method calls to the parent class
super() returns a proxy object that forwards method calls to the next class in the MRO, typically the parent class.
How do you create a static method in Python?
Answer: Use the @staticmethod decorator and no 'self' or 'cls' parameter
@staticmethod marks a method that takes neither 'self' nor 'cls', behaving like a regular function scoped to the class.
What is multiple inheritance in Python?
Answer: A class derives from more than one parent class
Python supports multiple inheritance with class Child(Parent1, Parent2):, and uses the MRO to resolve method lookups.