0
0
Pythonprogramming~20 mins

Modifying object state in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Modifying Object State
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code modifying object attributes?

Look at the code below. What will be printed after running it?

Python
class Box:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def resize(self, new_length, new_width):
        self.length = new_length
        self.width = new_width

box = Box(5, 3)
box.resize(10, 6)
print(box.length, box.width)
A10 3
B5 3
C10 6
D5 6
Attempts:
2 left
💡 Hint

Think about what the resize method does to the object's attributes.

Predict Output
intermediate
2:00remaining
What will be the value of the counter after method calls?

Consider this class that modifies its internal counter. What is the final value printed?

Python
class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1

    def reset(self):
        self.count = 0

c = Counter()
c.increment()
c.increment()
c.reset()
c.increment()
print(c.count)
A0
B1
C3
D2
Attempts:
2 left
💡 Hint

Follow the changes to count step by step.

Predict Output
advanced
2:00remaining
What is the output when modifying a mutable attribute inside a method?

What will this code print?

Python
class Basket:
    def __init__(self):
        self.items = []

    def add_item(self, item):
        self.items.append(item)

b = Basket()
b.add_item('apple')
b.add_item('banana')
print(b.items)
A['apple', 'banana']
B['apple']
C[]
D['banana']
Attempts:
2 left
💡 Hint

Think about how the list items changes when you append new elements.

Predict Output
advanced
2:00remaining
What error occurs when modifying a non-existent attribute?

What happens when this code runs?

Python
class Car:
    def __init__(self, model):
        self.model = model

car = Car('Sedan')
car.speed = 60
print(car.speed)
print(car.color)
A60 followed by AttributeError
B60 followed by 0
CAttributeError at car.speed assignment
D60 followed by null
Attempts:
2 left
💡 Hint

Check which attributes exist and which do not.

🧠 Conceptual
expert
3:00remaining
Which option correctly modifies the object's state to track history?

You want to keep track of all values assigned to an attribute value in an object. Which code correctly updates the history each time value changes?

A
class Tracker:
    def __init__(self):
        self.value = None
        self.history = []
    
    def update(self, new_value):
        self.value = new_value
        self.history = [new_value]
B
class Tracker:
    def __init__(self):
        self.value = None
        self.history = []
    
    def set_value(self, new_value):
        self.value = new_value
        self.history.append(new_value)
C
class Tracker:
    def __init__(self):
        self.value = None
        self.history = []
    
    def value(self, new_value):
        self.value = new_value
        self.history.append(new_value)
D
class Tracker:
    def __init__(self):
        self._value = None
        self.history = []
    
    @property
    def value(self):
        return self._value
    
    @value.setter
    def value(self, new_value):
        self._value = new_value
        self.history.append(new_value)
Attempts:
2 left
💡 Hint

Think about how to run code automatically when an attribute changes.