Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Class Methods and Static Methods
What will be the output of this code?
class MathOps:
    @staticmethod
    def add(x, y):
        return x + y

print(MathOps.add(3, 4))
ATypeError
B7
CNone
DError: missing self
Step-by-Step Solution
Solution:
  1. Step 1: Understand static method usage

    Static methods can be called on the class without an instance and do not require self.
  2. Step 2: Evaluate the method call

    add(3, 4) returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option B
  4. Quick Check:

    Static method call returns sum = B [OK]
Quick Trick: Static methods can be called directly on the class [OK]
Common Mistakes:
  • Expecting self parameter in static method
  • Confusing static method call with instance method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes