Challenge - 5 Problems
Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember, accessing an attribute with dot notation prints its value.
✗ Incorrect
The attribute 'brand' is set to 'Toyota' in the constructor. Accessing car.brand prints 'Toyota'.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
You can change attributes by assigning new values.
✗ Incorrect
The attribute 'name' was changed from 'Alice' to 'Bob'. So printing p.name outputs 'Bob'.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
If an attribute is not set, Python raises an error when accessed.
✗ Incorrect
The attribute 'age' was never set on the object. Accessing it raises AttributeError.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Class attributes are shared by all instances unless overridden.
✗ Incorrect
Changing Cat.species changes it for all instances. Both print statements output 'Felis catus'.
🧠 Conceptual
expert2: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)
Attempts:
2 left
💡 Hint
Slots restrict adding new attributes not listed.
✗ Incorrect
Because __slots__ limits attributes to 'value', adding 'new_attr' causes AttributeError.