Python Programming - Inheritance

Exercise : Inheritance - General Questions
  • Inheritance - General Questions
66.
How can you achieve method overloading?
By declaring methods with different return types
By defining multiple methods with the same name but different parameters
By explicitly specifying the data type of method parameters
Method overloading is not supported in Python
Answer: Option
Explanation:
Python does not support method overloading in the traditional sense as it is done in languages like Java. Methods with the same name in a class will override each other.

67.
Consider the following code:
class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        print("Dog barks")

def main():
    my_pet = Dog()
    my_pet.speak()
What will be the output of the main() function?
Animal speaks
Dog barks
The code will result in an error
Animal barks
Answer: Option
Explanation:
The speak() method is overridden in the Dog class, so calling speak() on a Dog instance will print "Dog barks".

68.
What is the purpose of the issubclass() function?
To check if an object is an instance of a class
To check if a class is a subclass of another class
To check if two classes have the same attributes
To check if an object has a specific attribute
Answer: Option
Explanation:
The issubclass() function is used to check if a class is a subclass of another class.

69.
Consider the following code:
class A:
    def method(self):
        print("Method in class A")

class B(A):
    def method(self):
        print("Method in class B")

class C(B):
    pass

obj = C()
obj.method()
What will be the output of the obj.method() call?
Method in class A
Method in class B
The code will result in an error
Method in class C
Answer: Option
Explanation:
The method() is overridden in class B, and since class C inherits from class B, calling obj.method() will print "Method in class B".

70.
How does Python handle multiple inheritance conflicts for method resolution?
By automatically resolving conflicts using the first defined method
By raising an error and requiring explicit resolution using super()
By using the C3 linearization algorithm to determine method resolution order
Multiple inheritance conflicts are not allowed in Python
Answer: Option
Explanation:
Python uses the C3 linearization algorithm to determine the method resolution order in case of conflicts in multiple inheritance.