Member-only story

Python OOPS Challenge | Day 8 |What is the output of following Python code?

Python Coding
Nov 4, 2024

--

In this code snippet, we have two classes: Fruit and Apple. The Apple class inherits from the Fruit class.

Code Analysis

1. The Fruit class has an __init__ method (constructor) that prints ‘1'.

2. The Apple class also has its own __init__ method that overrides the one from Fruit and prints ‘2'.

When we create an instance of Apple with obj = Apple(), Python will look for the __init__ method in the Apple class first. Since Apple has its own __init__ method, it overrides the __init__ method of Fruit. Therefore, only ‘2' is printed.

Output

The output of this code will be:

2

So, the correct answer is the second option: 2.

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet