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.
Pipe ‘A’ can fill a tank in 20 hours while pipe ‘B’ takes 30 hours to empty it. Pipe ‘A’ is opened alone when the tank was empty and after 1...
Tap A can fill a tank in 8 hours. Tap B can fill 12.5% part of the same tank in 2 hours, whereas Tap C can alone empty a tank in ‘x’ hours. 5/8 part...
Two pipes A and B can fill a tank in 24 min., and 32 min, respectively. If both the pipes are opened simultaneously, after how much time B should be clo...
- Pipe X can fill a pool in 18 hours and pipe Y in 36 hours. If both the pipes are opened in an empty pool, how much time will they take to fill it?
Two pipes, A and B, can independently fill the tank in 20 minutes and 25 minutes respectively, while pipe C can empty 7 gallons from the tank in 5 minut...
Pipe A fills a tank in 60 minutes, and pipe B can do it in 90 minutes. There’s also a pipe C that drains the tank in 180 minutes. If all three pipes a...
Pipe 'A' takes 20 minutes to fill half of the tank. Pipe 'B' is twice as efficient as pipe 'A'. Find the percentage of the tank filled by both in 8 minu...
Two pipes 'P' and 'Q' together can fill a tank in 16 minutes while 'Q' alone can fill the tank in 24 minutes. How much time does pipe 'P' alone need to ...
Pipe ‘A’ can fill a tank in 30 hours whereas leak ‘A’ can empty it in 45 hours. If they both operate along with pipe ‘B’, then the given tan...
- Pipe 'A' can fill a tank in 20 hours. Pipe 'B' is ____% as efficient as pipe 'A'. If both pipes are opened simultaneously, the tank will be completely full...