Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
Python - Class Methods and Static Methods
Identify the error in this code snippet:
class Calculator:
    @staticmethod
    def add(x, y):
        return x + y

    @classmethod
    def multiply(cls, x, y):
        return x * y

print(Calculator.add(2, 3))
print(Calculator.multiply(2, 3))
ANo error, code runs correctly
BClass method 'multiply' missing 'self' parameter
CStatic method 'add' missing 'self' parameter
DClass method 'multiply' missing 'cls' parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check static method definition

    Static methods do not require 'self' or 'cls'; 'add' is correctly defined with two parameters.
  2. Step 2: Check class method definition

    Class method 'multiply' correctly has 'cls' as the first parameter and two others; usage is correct.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Static and class methods correctly defined [OK]
Quick Trick: Static methods no 'self'; class methods need 'cls' first [OK]
Common Mistakes:
  • Expecting 'self' in static methods
  • Confusing 'self' and 'cls' in class methods
  • Assuming method calls require instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes