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.
A football team plays 60 matches in a tournament. Where 5 points are given to a team for winning a match and 2 points are subtracted for losing every ...
What is the difference between the average number of boys participating in ‘Tennis’ in both the schools together and average number of girls par...
In Delhi Public School, what is the respective ratio between the total number of participants (both boys and girls) who participates in ‘Tennis’ and...
If O is the origin and the coordinates of P are (2, 3, −4), then find the equation of the plane passing through P and perpendicular to OP.
Supporters of Mumbai in the crowd finally is approximately what percent of the initial number of supporters of Pune in the beginning at 8.00 pm?
What is the average number of passengers travelling in general class, sleeper class by both the trains?
In an election, a voter may vote for any number of candidates not greater than the number to be chosen. There are 8 candidates and 5 members are to be c...
What is the total number of Astronauts who were going to MOON?
What is the number of Astronauts who were going to more than one mission?
The total number of passengers in general class of train Q and 3 AC coach of train P together is approximately what percent of the total number of passe...