What is the output of the following code:
class Parent:
def show(self):
print('Parent method')
class Child(Parent):
pass
obj = Child()
obj.show()
def show(self):
print('Parent method')
class Child(Parent):
pass
obj = Child()
obj.show()
A. Parent method
B. An error will occur
C. Child method
D. obj
Answer: Option A
Solution (By Examveda Team)
The correct answer is A: Parent methodHere's why:
We have a class called `Parent` that has a method named `show`. This method prints "Parent method".
Then, we create another class called `Child` that inherits from the `Parent` class. This means the `Child` class automatically gets all the methods and attributes of the `Parent` class.
Importantly, the `Child` class doesn't define its own `show` method. It inherits the `show` method directly from `Parent`.
We create an object of the `Child` class called `obj`.
When we call `obj.show()`, Python looks for a `show` method in the `Child` class. Since it doesn't find one there, it looks in the `Parent` class (because `Child` inherits from `Parent`).
It finds the `show` method in the `Parent` class and executes it. Therefore, it prints "Parent method".
Join The Discussion
Comments (1)
Related Questions on Classes and Objects in Python
A. A blueprint for creating objects
B. A built-in function
C. A specific data type
D. A variable in Python
How do you define a class in Python?
A. Using the class keyword
B. Using the define keyword
C. Using the classdef keyword
D. Using the class method
A. Instances of a class
B. Built-in functions
C. Special data types
D. Global variables
How do you create an object of a class in Python?
A. Using the class name and parentheses
B. Using the create() method
C. Using the new keyword
D. Using the object() method

Wrong correct option is A.
I have tested the code by running it.