Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Python - Class Methods and Static Methods
Consider this code:
class Calculator:
    @staticmethod
    def multiply(x, y):
        return x * y

result = Calculator.multiply(5, 6)
print(result)

How would you modify this code to also allow calling multiply from an instance without changing the method definition?
AChange @staticmethod to @classmethod and add 'cls' parameter.
BAdd 'self' parameter to multiply method.
CNo change needed; static methods can be called from instances.
DDefine multiply outside the class.
Step-by-Step Solution
Solution:
  1. Step 1: Recall static method call rules

    Static methods can be called on class or instance without changes.
  2. Step 2: Verify if modification is needed

    Since multiply is static, calling from instance works as is.
  3. Final Answer:

    No change needed; static methods can be called from instances. -> Option C
  4. Quick Check:

    Static methods callable from instance without change [OK]
Quick Trick: Static methods callable from instance or class without change [OK]
Common Mistakes:
  • Adding 'self' to static methods unnecessarily
  • Changing static to class method without need
  • Thinking static methods can't be called from instances

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes