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.
The MRP of an article is Rs.1750 and the cost price of the article is 40% of its marked price. If x% of the discount offered on the marked price of the ...
A total of 444 chocolates are distributed among P, Q, and R in the ratio 3:5:4. If the distribution were instead done in the rati...
A and B partnered to start a business with initial investments of Rs. 1,200 and Rs. 1,500, respectively. After one year, they both reinvested additional...
In the each question, the given two series following the same pattern, then find the pattern used in the 1st series and use the pattern to find the unk...
A complex number z lies on or within a circle centered at (2, 0) with radius 5 units. What is the maximum value of |z – 2|?
2, 5, 16, 65, ?, 1957
Let vector r lie in the plane of p = i + j and q = j + k, and be such that r is perpendicular to p and satisfies r · q = 2. Then, the value of the dot ...
In a 1500-metre race, Arjun beats Bishnu by 600 metres, and Bishnu beats Chintu by 700 metres. How far will Arjun beat Chintu by ...
A vehicle travels 100 km at a speed of 50 km/h and 200 km at a speed of 75 km/h. What is the average speed for the entire trip?