Question
Consider the following Python code: class
Shape:   def area(self):     return 0 class Circle(Shape):   def __ init __ (self, radius):     self.radius = radius   def area(self):     return 3.14 * self.radius * self.radius s = Shape() c = Circle(5) print(s.area()) print(c.area()) What is the output of this code?Solution
This code demonstrates polymorphism in Python. 1. Base Class Behavior: The Shape class has an area method returning 0 by default, which is inherited but overridden by the Circle class. 2. Derived Class Behavior: The Circle class redefines the area method to calculate the area of a circle using the formula πr² . 3. Output: o The first call (s.area()) invokes the area method in Shape, returning 0. o The second call (c.area()) invokes the overridden area method in Circle, calculating and returning 78.5. This demonstrates inheritance and method overriding. Why Other Options Are Incorrect: • B) 0 followed by 0: The second call correctly calculates the circle's area. • C) 78.5 followed by 78.5: The first call uses the Shape class's method, which returns 0. • D) None followed by 78.5: The area method in Shape returns 0, not None. • E) Throws an error due to missing area method in Shape: The Shape class defines the area method.
According to the India State Forest Report 2021, which state has shown the highest increase in forest cover?
International labour organization convention 177 was recently seen in the news, is related to?
A shopkeeper sold a laptop after giving two successive discounts of 20% and 5% while making a profit of 30%. Find the approximate...
Auditors use ______ to gather information and determine if they need to conduct substantive testing. Sometimes, they can use analytical methods alone t...
Consider the following statements :
1. Falkland Islands are situated in Pacific Ocean.
2. Red Sea separates Sudan from Egypt.
Ratio of the work done by P, Q and R in one day is 4:2:5 respectively. They all together can complete the work in 35 days. Q and R worked on it for 23 d...
Goods returned by customer will be debited to which account?
How many lines other than those shown in the figure are required to join each corner with another?
Which of the following numbers is divisible by 4?
Which of the following statements is/are incorrect in regards to Green development?
1.At the UNFCCC COP27, Shri Narendra Modi announced Mission L...