0
0
Pythonprogramming~20 mins

Use cases for each method type in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Method Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Identify the use case of a static method
Which scenario best describes when to use a static method in a Python class?
AWhen the method needs to access or modify the class's attributes
BWhen the method performs a utility function that does not access instance or class data
CWhen the method is used to initialize a new instance of the class
DWhen the method needs to access or modify the instance's attributes
Attempts:
2 left
💡 Hint
Think about methods that don't need any information from the class or its objects.
Predict Output
intermediate
2: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)
A2
B1
C0
DError
Attempts:
2 left
💡 Hint
Class methods can modify class-level data.
Predict Output
intermediate
2: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())
AHello, !
BHello, name!
CHello, Alice!
DError
Attempts:
2 left
💡 Hint
Instance methods use self to access data.
🧠 Conceptual
advanced
2:00remaining
Choosing method type for factory pattern
Which method type is best suited to implement a factory method that creates instances of the class?
AInstance method
BStatic method
CProperty method
DClass method
Attempts:
2 left
💡 Hint
Factory methods often need to create instances and may need class information.
Predict Output
expert
3: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())
A
Child static
Child class overridden
Child instance
B
Base static
Child class overridden
Base instance
C
Child static
Base class
Child instance
D
Base static
Base class
Base instance
Attempts:
2 left
💡 Hint
Look at which methods are overridden and how they are called.