0
0
Pythonprogramming~20 mins

Static methods behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of calling static method from instance
What is the output of this code when calling obj.greet()?
Python
class MyClass:
    @staticmethod
    def greet():
        return "Hello from static method"

obj = MyClass()
print(obj.greet())
AAttributeError: 'MyClass' object has no attribute 'greet'
BTypeError: greet() takes 0 positional arguments but 1 was given
CHello from static method
DNone
Attempts:
2 left
💡 Hint
Static methods do not receive the instance automatically.
Predict Output
intermediate
2:00remaining
Static method accessing class variable
What will be printed when this code runs?
Python
class Counter:
    count = 0

    @staticmethod
    def increment():
        Counter.count += 1

Counter.increment()
Counter.increment()
print(Counter.count)
ANameError
B0
CTypeError
D2
Attempts:
2 left
💡 Hint
Static methods can access class variables by using the class name.
Predict Output
advanced
2:00remaining
Static method vs class method behavior
What is the output of this code?
Python
class Example:
    value = 5

    @staticmethod
    def static_method():
        return Example.value

    @classmethod
    def class_method(cls):
        return cls.value

print(Example.static_method())
print(Example.class_method())
A5\n5
B5\nTypeError
CTypeError\n5
DAttributeError\nAttributeError
Attempts:
2 left
💡 Hint
Static methods use the class name explicitly; class methods receive the class as first argument.
Predict Output
advanced
2:00remaining
Static method called with instance and class
What will be the output of this code?
Python
class Demo:
    @staticmethod
    def info():
        return "Static method called"

obj = Demo()
print(Demo.info())
print(obj.info())
AStatic method called\nStatic method called
BAttributeError\nAttributeError
CTypeError\nStatic method called
DStatic method called\nTypeError
Attempts:
2 left
💡 Hint
Static methods can be called both on the class and on instances without error.
Predict Output
expert
2:00remaining
Static method with parameters and no self or cls
What is the output of this code?
Python
class Calculator:
    @staticmethod
    def multiply(a, b):
        return a * b

print(Calculator.multiply(3, 4))
calc = Calculator()
print(calc.multiply(5, 6))
ATypeError\n30
B12\n30
C12\nTypeError
DAttributeError\nAttributeError
Attempts:
2 left
💡 Hint
Static methods behave like normal functions inside the class and do not receive instance or class automatically.