0
0
Pythonprogramming~20 mins

Real-world modeling using objects in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method calls on a class instance

Consider the following Python class modeling a simple bank account. What will be the output after running the code?

Python
class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
        return self.balance
    
    def withdraw(self, amount):
        if amount > self.balance:
            return "Insufficient funds"
        self.balance -= amount
        return self.balance

account = BankAccount("Alice", 100)
print(account.deposit(50))
print(account.withdraw(30))
print(account.withdraw(150))
A
100
50
Insufficient funds
B
150
70
Insufficient funds
C
150
70
-50
D
150
120
Insufficient funds
Attempts:
2 left
💡 Hint

Think about how the balance changes after each method call.

🧠 Conceptual
intermediate
1:30remaining
Understanding object attributes and methods

Which statement about Python objects is correct?

AAn object's methods can only access variables defined inside the method itself.
BEach object instance has its own copy of instance variables.
CInstance variables are shared across all instances of a class.
DClass variables are unique to each object instance.
Attempts:
2 left
💡 Hint

Think about what happens when you create multiple objects from the same class.

Predict Output
advanced
2:30remaining
Output of inheritance and method overriding

What will be printed when the following code runs?

Python
class Vehicle:
    def __init__(self, brand):
        self.brand = brand
    def start(self):
        return f"{self.brand} vehicle started"

class Car(Vehicle):
    def start(self):
        return f"{self.brand} car started with key"

class ElectricCar(Car):
    def start(self):
        return f"{self.brand} electric car started silently"

v = Vehicle("Generic")
c = Car("Toyota")
e = ElectricCar("Tesla")
print(v.start())
print(c.start())
print(e.start())
A
Generic vehicle started
Toyota car started with key
Tesla electric car started silently
B
Generic vehicle started
Toyota vehicle started
Tesla car started with key
C
Generic vehicle started
Toyota car started with key
Tesla vehicle started
D
Generic vehicle started
Toyota electric car started silently
Tesla electric car started silently
Attempts:
2 left
💡 Hint

Look at how each class overrides the start method.

🔧 Debug
advanced
1:30remaining
Identify the error in object initialization

What error will this code produce when run?

Python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person("Bob")
ATypeError: __init__() missing 1 required positional argument: 'age'
BNameError: name 'age' is not defined
CAttributeError: 'Person' object has no attribute 'age'
DNo error, object created successfully
Attempts:
2 left
💡 Hint

Check how many arguments the constructor expects versus how many are given.

🚀 Application
expert
3:00remaining
Calculate total price with object composition

Given the classes below modeling products and a shopping cart, what is the output of the final print statement?

Python
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

class ShoppingCart:
    def __init__(self):
        self.items = []
    
    def add_product(self, product, quantity):
        self.items.append((product, quantity))
    
    def total_price(self):
        total = 0
        for product, quantity in self.items:
            total += product.price * quantity
        return total

p1 = Product("Book", 12.99)
p2 = Product("Pen", 1.50)
cart = ShoppingCart()
cart.add_product(p1, 3)
cart.add_product(p2, 10)
print(f"Total: ${cart.total_price():.2f}")
ATotal: $51.99
BTotal: $49.47
CTotal: $53.97
DTotal: $60.00
Attempts:
2 left
💡 Hint

Multiply each product's price by its quantity and add all together.