0
0
Pythonprogramming~20 mins

Instance methods in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.