Hackerrank Object-Oriented Programming Basics 3 — Questions and Answers
Question 1: How do you define a class method (not an instance method) in Python?
- Use @staticmethod decorator with 'cls' as first parameter
- Use @classmethod decorator with 'cls' as first parameter (Correct answer)
- Use @classmethod decorator with 'self' as first parameter
- Define the method outside the class body
Correct 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.
Question 2: What is method overriding in Python?
- Defining multiple methods with the same name but different parameter counts
- A child class provides its own implementation of a method inherited from the parent (Correct answer)
- Deleting a method from the parent class
- Adding parameters to a method at runtime
Correct 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.
Question 3: What is the output of: class A: x = 10 a1 = A() A.x = 20 print(a1.x)
- 10
- 20 (Correct answer)
- None
- AttributeError
Correct 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.
Question 4: Which statement about Python's __str__ method is correct?
- It returns an integer representation of the object
- It is called by str() and print() to get a human-readable string (Correct answer)
- It compares two objects for equality
- It returns the memory address of the object
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).
Question 5: What does super() do in a subclass?
- Creates a new instance of the parent class
- Returns a proxy that delegates method calls to the parent class (Correct answer)
- Deletes the child class and keeps only the parent
- Copies all parent attributes to the child instance
Correct 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.
Question 6: How do you create a static method in Python?
- Use the keyword 'static' before 'def'
- Use the @staticmethod decorator and no 'self' or 'cls' parameter (Correct answer)
- Use the @classmethod decorator without parameters
- Define the method outside the class
Correct 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.
Question 7: What is multiple inheritance in Python?
- A class can have multiple __init__ methods
- A class derives from more than one parent class (Correct answer)
- An instance can belong to multiple classes simultaneously
- A method is defined multiple times in the same class
Correct 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.
How do you define a class method (not an instance method) in Python?