Bird
Raised Fist0
Pythonprogramming~20 mins

Instance methods in Python - Practice Problems & Coding Challenges

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
🎖️
Instance Methods 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 Python code?
Python
class Car:
    def __init__(self, brand):
        self.brand = brand
    def show_brand(self):
        return f"This car is a {self.brand}."

my_car = Car("Toyota")
print(my_car.show_brand())
AError: show_brand() missing 1 required positional argument
BThis car is a Toyota.
CToyota
DThis car is a brand.
Attempts:
2 left
💡 Hint
Look at how the instance method uses self to access the brand attribute.
Predict Output
intermediate
2:00remaining
Instance method modifying attribute
What will be the value of x.speed after running this code?
Python
class Bike:
    def __init__(self):
        self.speed = 0
    def accelerate(self, amount):
        self.speed += amount

x = Bike()
x.accelerate(10)
A10
B0
CError: accelerate() missing 1 required positional argument
DNone
Attempts:
2 left
💡 Hint
The accelerate method adds the amount to the speed attribute.
Predict Output
advanced
2:00remaining
Instance method with default argument
What is the output of this code snippet?
Python
class Counter:
    def __init__(self):
        self.count = 0
    def add(self, n=1):
        self.count += n
        return self.count

c = Counter()
print(c.add())
print(c.add(5))
A0\n5
B1\n1
C1\n6
DError: add() missing 1 required positional argument
Attempts:
2 left
💡 Hint
The add method increases count by n, defaulting to 1 if no argument is given.
Predict Output
advanced
2:00remaining
Instance method calling another instance method
What will this code print?
Python
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        return f"Hello, {self.name}!"
    def welcome(self):
        return self.greet() + " Welcome to the platform."

p = Person("Alice")
print(p.welcome())
AAlice Welcome to the platform.
BHello, welcome to the platform.
CError: greet() missing 1 required positional argument
DHello, Alice! Welcome to the platform.
Attempts:
2 left
💡 Hint
The welcome method calls greet and adds more text.
🧠 Conceptual
expert
2:00remaining
Understanding self in instance methods
Which statement about the use of self in instance methods is correct?
Aself refers to the instance on which the method is called and is passed automatically.
Bself is a keyword in Python that refers to the current class.
Cself must be explicitly passed when calling an instance method.
Dself is optional and can be omitted in instance method definitions.
Attempts:
2 left
💡 Hint
Think about how Python passes the instance to methods.

Practice

(1/5)
1. What is the purpose of the self parameter in an instance method?
easy
A. It refers to the specific object calling the method.
B. It is used to define a static method.
C. It is a keyword to create a new object.
D. It stores the return value of the method.

Solution

  1. Step 1: Understand what self represents

    self is a reference to the current object that calls the method, allowing access to its attributes and other methods.
  2. Step 2: Differentiate from other options

    Static methods, object creation, and return values are unrelated concepts, which are not the role of self.
  3. Final Answer:

    It refers to the specific object calling the method. -> Option A
  4. Quick Check:

    self = current object [OK]
Hint: Remember: self means 'this object' inside methods [OK]
Common Mistakes:
  • Thinking self is a keyword, not a parameter
  • Confusing self with class or static methods
  • Assuming self is optional in instance methods
2. Which of the following is the correct way to define an instance method inside a Python class?
easy
A. def method_name():
B. def method_name(*args):
C. def method_name(cls):
D. def method_name(self):

Solution

  1. Step 1: Recall instance method syntax

    Instance methods must have self as the first parameter to access the object's data.
  2. Step 2: Check each option

    def method_name(): misses self, def method_name(cls): uses cls which is for class methods, and def method_name(*args): uses a generic parameter which is not standard for instance methods.
  3. Final Answer:

    def method_name(self): -> Option D
  4. Quick Check:

    Instance method = first param self [OK]
Hint: Instance methods always start with self parameter [OK]
Common Mistakes:
  • Omitting self in method definition
  • Using cls instead of self for instance methods
  • Using no parameters or *args incorrectly
3. What will be the output of this code?
class Dog:
    def __init__(self, name):
        self.name = name
    def bark(self):
        return f"{self.name} says Woof!"

my_dog = Dog('Buddy')
print(my_dog.bark())
medium
A. Woof!
B. Buddy says Woof!
C. my_dog says Woof!
D. Error: missing self parameter

Solution

  1. Step 1: Understand object creation and method call

    The object my_dog is created with name 'Buddy'. Calling bark() uses self.name which is 'Buddy'.
  2. Step 2: Evaluate the return value

    The method returns the string "Buddy says Woof!" which is printed.
  3. Final Answer:

    Buddy says Woof! -> Option B
  4. Quick Check:

    Method uses self.name = Buddy [OK]
Hint: Instance methods use self to access object data [OK]
Common Mistakes:
  • Ignoring self and expecting just 'Woof!'
  • Confusing variable name with object name
  • Assuming method returns nothing
4. Find the error in this code:
class Car:
    def __init__(self, model):
        self.model = model
    def show_model():
        print(f"Model: {self.model}")

car = Car('Tesla')
car.show_model()
medium
A. Cannot create object without arguments
B. Wrong attribute name used
C. Missing self parameter in show_model method
D. print statement syntax error

Solution

  1. Step 1: Check method definition

    The method show_model is missing the self parameter, so it cannot access instance attributes.
  2. Step 2: Understand the error cause

    Calling car.show_model() passes the object automatically, but method lacks self to receive it, causing a TypeError.
  3. Final Answer:

    Missing self parameter in show_model method -> Option C
  4. Quick Check:

    Instance methods need self parameter [OK]
Hint: Always include self as first parameter in instance methods [OK]
Common Mistakes:
  • Forgetting self in method definition
  • Trying to access self without parameter
  • Confusing class and instance methods
5. You want to create a class Counter that counts how many times its method increment is called on each object separately. Which code correctly implements this behavior?
hard
A. class Counter: def __init__(self): self.count = 0 def increment(self): self.count += 1 def get_count(self): return self.count
B. class Counter: count = 0 def increment(self): Counter.count += 1 def get_count(self): return Counter.count
C. class Counter: def __init__(self): self.count = 0 def increment(): self.count += 1 def get_count(self): return self.count
D. class Counter: def __init__(self): self.count = 0 def increment(self): count += 1 def get_count(self): return self.count

Solution

  1. Step 1: Understand instance vs class variables

    Instance variables (self.count) ensure each object tracks its own count separately. Methods must accept self and update self.count.
  2. Step 2: Eliminate incorrect approaches

    Class variables are shared across all instances. Missing self parameter in methods causes TypeError. Updating a local variable doesn't affect the instance attribute.
  3. Final Answer:

    class Counter: def __init__(self): self.count = 0 def increment(self): self.count += 1 def get_count(self): return self.count -> Option A
  4. Quick Check:

    Instance variables + self = separate counts [OK]
Hint: Use self.variable for per-object data, not class variables [OK]
Common Mistakes:
  • Using class variables for per-object data
  • Forgetting self in method parameters
  • Incrementing local variables instead of instance attributes