Challenge - 5 Problems
Master of Method Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Identify the use case of a static method
Which scenario best describes when to use a static method in a Python class?
Attempts:
2 left
💡 Hint
Think about methods that don't need any information from the class or its objects.
✗ Incorrect
A static method is used for utility functions that don't need to access instance or class data. It behaves like a regular function but lives inside the class for organization.
❓ Predict Output
intermediate2:00remaining
Output of a class method modifying class attribute
What is the output of this code?
Python
class Counter: count = 0 @classmethod def increment(cls): cls.count += 1 Counter.increment() Counter.increment() print(Counter.count)
Attempts:
2 left
💡 Hint
Class methods can modify class-level data.
✗ Incorrect
The class method increment increases the class attribute count by 1 each time it is called. After two calls, count becomes 2.
❓ Predict Output
intermediate2:00remaining
Output of instance method accessing instance data
What will this code print?
Python
class Person: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!" p = Person('Alice') print(p.greet())
Attempts:
2 left
💡 Hint
Instance methods use self to access data.
✗ Incorrect
The instance method greet accesses the instance attribute name via self and returns a greeting with the actual name.
🧠 Conceptual
advanced2:00remaining
Choosing method type for factory pattern
Which method type is best suited to implement a factory method that creates instances of the class?
Attempts:
2 left
💡 Hint
Factory methods often need to create instances and may need class information.
✗ Incorrect
Class methods receive the class as the first argument and are commonly used as factory methods to create instances, possibly with different parameters or subclasses.
❓ Predict Output
expert3:00remaining
Output of mixed method calls in inheritance
What is the output of this code?
Python
class Base: @staticmethod def static_method(): return 'Base static' @classmethod def class_method(cls): return f'{cls.__name__} class' def instance_method(self): return 'Base instance' class Child(Base): @staticmethod def static_method(): return 'Child static' @classmethod def class_method(cls): return f'{cls.__name__} class overridden' def instance_method(self): return 'Child instance' c = Child() print(c.static_method()) print(c.class_method()) print(c.instance_method())
Attempts:
2 left
💡 Hint
Look at which methods are overridden and how they are called.
✗ Incorrect
The Child class overrides all three methods. Calling them on an instance of Child uses the overridden versions, so the output reflects the Child implementations.