0
0
Pythonprogramming~20 mins

Accessing and modifying attributes in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Attribute Mastery
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 attribute access code?
Consider this Python class and code. What will be printed?
Python
class Car:
    def __init__(self, brand):
        self.brand = brand

car = Car('Toyota')
print(car.brand)
ABrand
Bcar.brand
CToyota
DError: attribute not found
Attempts:
2 left
💡 Hint
Remember, accessing an attribute with dot notation prints its value.
Predict Output
intermediate
2:00remaining
What happens when modifying an attribute?
What will be the output after modifying the attribute?
Python
class Person:
    def __init__(self, name):
        self.name = name

p = Person('Alice')
p.name = 'Bob'
print(p.name)
ABob
BNone
CError: cannot modify attribute
DAlice
Attempts:
2 left
💡 Hint
You can change attributes by assigning new values.
Predict Output
advanced
2:00remaining
What is the output when accessing a missing attribute?
What happens when you try to access an attribute that does not exist?
Python
class Dog:
    def __init__(self, name):
        self.name = name

d = Dog('Rex')
print(d.age)
AAttributeError
BNone
CRex
D0
Attempts:
2 left
💡 Hint
If an attribute is not set, Python raises an error when accessed.
Predict Output
advanced
2:00remaining
What is the output after modifying a class attribute?
What will this code print?
Python
class Cat:
    species = 'Felis'

c1 = Cat()
c2 = Cat()
Cat.species = 'Felis catus'
print(c1.species)
print(c2.species)
AFelis\nFelis
BFelis\nFelis catus
CError: cannot modify class attribute
DFelis catus\nFelis catus
Attempts:
2 left
💡 Hint
Class attributes are shared by all instances unless overridden.
🧠 Conceptual
expert
2:00remaining
Which option causes an error when modifying attributes?
Given this code, which option will cause an error when trying to modify the attribute?
Python
class Immutable:
    __slots__ = ['value']
    def __init__(self, value):
        self.value = value

obj = Immutable(10)
Aobj.value = 20
Bobj.new_attr = 30
Cdel obj.value
Dprint(obj.value)
Attempts:
2 left
💡 Hint
Slots restrict adding new attributes not listed.