Challenge - 5 Problems
Static Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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())
Attempts:
2 left
💡 Hint
Static methods do not receive the instance automatically.
✗ Incorrect
Static methods can be called on instances without passing the instance as an argument. They behave like regular functions inside the class namespace.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Static methods can access class variables by using the class name.
✗ Incorrect
The static method increments the class variable 'count' twice, so the final value is 2.
❓ Predict Output
advanced2: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())
Attempts:
2 left
💡 Hint
Static methods use the class name explicitly; class methods receive the class as first argument.
✗ Incorrect
Both methods return the class variable 'value' correctly, so both print 5.
❓ Predict Output
advanced2: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())
Attempts:
2 left
💡 Hint
Static methods can be called both on the class and on instances without error.
✗ Incorrect
Static methods do not depend on instance or class arguments, so both calls succeed and print the same string.
❓ Predict Output
expert2: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))
Attempts:
2 left
💡 Hint
Static methods behave like normal functions inside the class and do not receive instance or class automatically.
✗ Incorrect
Both calls pass two arguments explicitly, so both calls return the product correctly.