Skip to content
pythonintermediate

Python Object-Oriented Programming

Classes, inheritance, polymorphism, magic methods and properties

7 questions

By EZ4Code Team

1. Which of the following descriptions of Python's __init__ method is correct?

It is the class constructor, automatically called when creating an instance
It is the class destructor, called when destroying an instance
It must return a value
It cannot take parameters
Explanation: __init__ is the initialization method (constructor), automatically called when instantiating an object, used to initialize instance attributes, and does not need to return a value.

2. Regarding inheritance, what does the following code output? class A: def speak(self): return 'A' class B(A): def speak(self): return 'B' b = B() print(b.speak())

class A:
    def speak(self):
        return 'A'

class B(A):
    def speak(self):
        return 'B'

b = B()
print(b.speak())
A
B
AB
Error
Explanation: Subclass B overrides the speak method of parent class A. The Method Resolution Order (MRO) prioritizes the subclass method, so it outputs 'B'.

3. What is the main purpose of using super()?

To call the parent class method, avoiding hardcoding the parent class name
To create a new object
To access private attributes
To delete an instance
Explanation: super() is used to call parent class methods, follows the MRO in multiple inheritance, and avoids hardcoding parent class names for easier maintenance.

4. Which of the following is the correct way to define a class method (classmethod)?

class MyClass:
    @classmethod
    def foo(cls):
        pass
Use the @classmethod decorator, with cls as the first parameter
Use the @staticmethod decorator, with self as the first parameter
Use the @property decorator
No decorator needed
Explanation: Class methods use the @classmethod decorator, with the first parameter being the class itself (conventionally cls), which can access and modify class-level state.

5. What does the @property decorator do?

Converts a method into read-only property access
Declares a static variable
Declares an abstract method
Declares a private method
Explanation: @property wraps a method as a property, accessible via obj.name, often combined with @name.setter for controllable read/write access.

6. What is the difference between the magic methods __str__ and __repr__?

__str__ is user-readable, __repr__ is developer-oriented and strives to be unambiguous
They are completely identical
__repr__ is user-oriented, __str__ is developer-oriented
Only __str__ exists
Explanation: __str__ returns a user-friendly string (used by print); __repr__ returns the official string (used for debugging, repr()), and should ideally be able to reproduce the object.

7. Regarding polymorphism, which statement is correct?

Objects of different classes can achieve polymorphism by implementing methods with the same name, without needing to inherit from the same parent class
Polymorphism must rely on a strong type system
Python does not support polymorphism
Polymorphism can only be achieved through abstract base classes
Explanation: Python is a dynamic language; polymorphism is based on duck typing: as long as an object implements the required methods, it doesn't need to inherit from the same parent class.

More python Quizzes