Bird
Raised Fist0
Pythonprogramming~20 mins

Difference between method types in Python - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of instance method call
What is the output of this code when calling obj.instance_method()?
Python
class MyClass:
    def instance_method(self):
        return 'instance method called'

obj = MyClass()
print(obj.instance_method())
ATypeError: instance_method() missing 1 required positional argument: 'self'
Binstance method called
CMyClass.instance_method
DNone
Attempts:
2 left
💡 Hint
Instance methods receive the instance automatically as the first argument.
Predict Output
intermediate
2:00remaining
Output of class method call
What is the output of this code when calling MyClass.class_method()?
Python
class MyClass:
    @classmethod
    def class_method(cls):
        return f'class method called from {cls.__name__}'

print(MyClass.class_method())
ANone
BTypeError: class_method() missing 1 required positional argument: 'cls'
Cclass method called from MyClass
Dclass method called from cls
Attempts:
2 left
💡 Hint
Class methods receive the class itself as the first argument.
Predict Output
advanced
2:00remaining
Output of static method call
What is the output of this code when calling MyClass.static_method()?
Python
class MyClass:
    @staticmethod
    def static_method():
        return 'static method called'

print(MyClass.static_method())
Astatic method called
BTypeError: static_method() missing 1 required positional argument
CMyClass.static_method
DNone
Attempts:
2 left
💡 Hint
Static methods do not receive any automatic first argument.
Predict Output
advanced
2:00remaining
Error when calling instance method on class
What error occurs when calling MyClass.instance_method() without an instance?
Python
class MyClass:
    def instance_method(self):
        return 'instance method called'

print(MyClass.instance_method())
ATypeError: instance_method() missing 1 required positional argument: 'self'
BAttributeError: 'MyClass' object has no attribute 'instance_method'
Cinstance method called
DNone
Attempts:
2 left
💡 Hint
Instance methods require an instance to be called.
🧠 Conceptual
expert
2:00remaining
Identify method type from behavior
Which method type does NOT receive any automatic first argument and behaves like a regular function inside the class?
AInstance method
BClass method
CAbstract method
DStatic method
Attempts:
2 left
💡 Hint
Think about which method type does not get self or cls automatically.

Practice

(1/5)
1. Which method type in Python automatically receives the instance as the first argument named self?
easy
A. Instance method
B. Class method
C. Static method
D. Global function

Solution

  1. Step 1: Understand method types

    Instance methods receive the instance as the first argument, usually named self.
  2. Step 2: Identify method with self

    Class methods receive the class as cls, static methods receive no automatic first argument.
  3. Final Answer:

    Instance method -> Option A
  4. Quick Check:

    Method with self = Instance method [OK]
Hint: Look for self as first parameter for instance methods [OK]
Common Mistakes:
  • Confusing cls with self
  • Thinking static methods have self
  • Mixing global functions with methods
2. Which decorator is used to define a class method in Python?
easy
A. @classmethod
B. @staticmethod
C. @instance
D. @class

Solution

  1. Step 1: Recall Python decorators for methods

    Class methods use the @classmethod decorator to receive the class as the first argument.
  2. Step 2: Identify correct decorator

    @staticmethod is for static methods, @instance and @class are invalid decorators.
  3. Final Answer:

    @classmethod -> Option A
  4. Quick Check:

    Class method decorator = @classmethod [OK]
Hint: Class methods always use @classmethod decorator [OK]
Common Mistakes:
  • Using @staticmethod for class methods
  • Assuming @instance is a valid decorator
  • Confusing decorator names
3. What is the output of this code?
class MyClass:
    @staticmethod
    def greet():
        return 'Hello'

print(MyClass.greet())
medium
A. AttributeError
B. TypeError
C. None
D. 'Hello'

Solution

  1. Step 1: Understand static method behavior

    Static methods do not require an instance or class argument and can be called directly on the class.
  2. Step 2: Analyze the code output

    The static method greet returns the string 'Hello', so print(MyClass.greet()) outputs 'Hello'.
  3. Final Answer:

    'Hello' -> Option D
  4. Quick Check:

    Static method call returns 'Hello' [OK]
Hint: Static methods can be called on class without arguments [OK]
Common Mistakes:
  • Expecting a TypeError for missing self
  • Confusing static with instance method call
  • Thinking static methods return None by default
4. Identify the error in this code:
class Example:
    @classmethod
    def show(cls):
        print(cls.value)

Example.show()
medium
A. Missing @staticmethod decorator
B. Method should use 'self' instead of 'cls'
C. Missing class attribute 'value'
D. Syntax error in method definition

Solution

  1. Step 1: Check class method usage

    The method show is a class method and tries to print cls.value.
  2. Step 2: Verify class attribute existence

    The class Example does not define value, so accessing cls.value causes an AttributeError at runtime.
  3. Final Answer:

    Missing class attribute 'value' -> Option C
  4. Quick Check:

    Class attribute missing causes error [OK]
Hint: Class methods need class attributes to access via cls [OK]
Common Mistakes:
  • Confusing instance and class attributes
  • Thinking @staticmethod fixes attribute errors
  • Using 'self' in class methods incorrectly
5. You want a method that can be called on the class or instance but does NOT access instance or class data. Which method type should you use?
hard
A. Class method
B. Static method
C. Instance method
D. Property method

Solution

  1. Step 1: Understand method access types

    Instance methods access instance data via self, class methods access class data via cls.
  2. Step 2: Identify method without data access

    Static methods do not access instance or class data and can be called on both class and instance.
  3. Final Answer:

    Static method -> Option B
  4. Quick Check:

    No data access = Static method [OK]
Hint: No data access means use static method [OK]
Common Mistakes:
  • Choosing instance or class method incorrectly
  • Confusing property methods with static methods
  • Assuming static methods access class data