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.
इनमें से किस शब्द के पर्यायवाची गलत हैं ?
नीचे दिए गए शब्दों का सही हिंदी अनुवाद विकल्पों से चयन कर�...
दिए गए प्रत्येक प्रश्न में एक अंग्रेजी का वाक्य दिया गया �...
राजधानी दिल्ली किस भाषाई क्षेत्र में शामिल किया गया है ?
संसदीय राजभाषा समिति की किस उप-समिति द्वारा बैंकों का निर�...
कार्यान्वयन के लिए लिए सही पारिभाषिक शब्द है
नीचे दिए गए वाक्यों में से सही अनुवाद चुनिए:
(i) “Packing credit guara...
तेल देखो तेल की धार देखो का अर्थ है -
इनमे से क्या ‘संकल्प’ का सही अंग्रेजी पर्याय नही है?
नीचे दिए गए वाक्यों के सही अनुवाद का मिलान करें और उचित वि�...