Python Programming - Classes

Exercise : Classes - General Questions
  • Classes - General Questions
16.
What does the term "polymorphism" mean in the context of object-oriented programming?
It refers to the ability of a class to inherit from multiple classes.
It signifies the encapsulation of data and methods within a class.
It is the ability of different classes to be used interchangeably.
It defines the hierarchy of classes in a program.
Answer: Option
Explanation:
Polymorphism in object-oriented programming allows objects of different classes to be treated as objects of a common base class. It enables code to work with objects of various types through a common interface.

17.
In Python, what is the purpose of the @classmethod decorator in a subclass?
It creates a static method in the subclass.
It defines a class method specific to the subclass.
It indicates a method that takes both the class and instance as parameters.
It allows the subclass to override the method in the superclass.
Answer: Option
Explanation:
The @classmethod decorator in a subclass is used to define a class method that is specific to the subclass. It can be called on the class and is aware of the subclass.

18.
What is the purpose of the __eq__ method?
It is called when an object is equal to another object using the == operator.
It defines the equality check for class attributes.
It is used to compare the memory addresses of two objects.
It signifies the end of an object's lifecycle.
Answer: Option
Explanation:
The __eq__ method is called when an object is compared for equality using the == operator. It allows customizing the behavior of equality checks for objects.

19.
What is the purpose of the __len__ method?
It is called when an object is created.
It defines the length of a string attribute in a class.
It is used to calculate the length of an object, typically for sequences.
It signifies the length of an object's lifecycle.
Answer: Option
Explanation:
The __len__ method is used to define the behavior of the len() function on an object. It is commonly implemented for objects that represent sequences, allowing the determination of their length.

20.
In Python, what is the purpose of the __iter__ method in a class?
It initializes an iterator for the class.
It defines the behavior of the class when used in a loop.
It is called when an object is instantiated.
It indicates the inheritance hierarchy of the class.
Answer: Option
Explanation:
The __iter__ method is used to initialize an iterator for a class. It should return an object that implements the __next__ method, allowing the class to be used in a for loop.