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.
Five years ago, the age of A was 3 times the age of B. After 7 years, the difference between their ages will be 28 years. Find the present age of A.
The ratio of present age of A to that of B is 6:5 and the ratio of present age of A to that of C is 6:8. The average present age of A, B and C is 19 yea...
The ages of A and B are in the ratio 5:7. If the sum of their ages is 72 years, what will be A's age after 6 years?
Present age of ‘A’ is 40% more than that of ‘B’. If 11 years hence from now, ‘B’ will be 10 years younger than ‘A’, then find the sum of...
The current age of Derek is twice that of Eve. 7 years from now, Derek's age will be 1.5 times the age of Fred at that time. If Eve is 5 years younger t...
Five years ago, the ratio of the age of Tarun to that of Ayush was 6 : 11. Umesh is 10 years older than Tarun. Umesh is ten years younger than Ayush. Wh...
The ratio of the present ages of ‘A’ and ‘B’ is 5:6, respectively. Three years hence from now, the age of ‘B’ will be 21 years. If the avera...
The current ages of 'Pankaj' and 'Ranjan' are in the ratio of 2:5. If Ranjan's age 13 years from now will be six times Pankaj's age 6 years prior to now...
Ratio of present ages of ‘M’ and ‘N’ is 4:3 respectively. After 6 years, ratio of ages of ‘N’ and ‘M’ becomes 5:7 respectively. Find the...
The average age of three children in a family is 25% of the average of the age of father and the eldest child. The total age of the mother and the young...