Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Class Methods and Static Methods
What will be the output of this code?
class Circle:
    pi = 3.14

    def __init__(self, radius):
        self.radius = radius

    @classmethod
    def area(cls, radius):
        return cls.pi * radius * radius

print(Circle.area(5))
A78.5
BError: missing self
C15.7
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand class method usage

    The class method area uses cls.pi and the radius argument to calculate area.
  2. Step 2: Calculate area

    Area = 3.14 * 5 * 5 = 78.5, which is returned and printed.
  3. Final Answer:

    78.5 -> Option A
  4. Quick Check:

    Class method uses cls.pi correctly = 78.5 [OK]
Quick Trick: Class methods can use class variables without instance [OK]
Common Mistakes:
MISTAKES
  • Expecting self in class method
  • Confusing class and instance variables
  • Miscomputing area formula

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes