Challenge - 5 Problems
Class Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of class attribute access
What is the output of this code?
Python
class Dog: species = "Canis familiaris" def __init__(self, name): self.name = name dog1 = Dog("Buddy") dog2 = Dog("Max") print(dog1.species) print(dog2.species) print(Dog.species)
Attempts:
2 left
💡 Hint
Class attributes are shared by all instances unless overridden.
✗ Incorrect
The class attribute 'species' is shared by all instances and the class itself. Accessing it from instances or the class prints the same value.
❓ Predict Output
intermediate2:00remaining
Changing class attribute affects instances
What is the output of this code?
Python
class Car: wheels = 4 car1 = Car() car2 = Car() Car.wheels = 6 print(car1.wheels) print(car2.wheels)
Attempts:
2 left
💡 Hint
Changing a class attribute changes it for all instances that don't override it.
✗ Incorrect
Changing Car.wheels to 6 updates the class attribute. Both car1 and car2 see the updated value because they don't have an instance attribute named wheels.
❓ Predict Output
advanced2:00remaining
Instance attribute shadows class attribute
What is the output of this code?
Python
class Book: genre = "Fiction" b1 = Book() b2 = Book() b2.genre = "Non-Fiction" print(b1.genre) print(b2.genre) print(Book.genre)
Attempts:
2 left
💡 Hint
Instance attributes override class attributes with the same name.
✗ Incorrect
b2.genre creates an instance attribute that shadows the class attribute. b1.genre and Book.genre remain unchanged.
❓ Predict Output
advanced2:00remaining
Modifying mutable class attribute affects all instances
What is the output of this code?
Python
class Team: members = [] team1 = Team() team2 = Team() team1.members.append("Alice") print(team1.members) print(team2.members) print(Team.members)
Attempts:
2 left
💡 Hint
Mutable class attributes are shared by all instances.
✗ Incorrect
The list 'members' is a class attribute shared by all instances. Appending to it via one instance affects all.
🧠 Conceptual
expert2:30remaining
Class attribute behavior with inheritance
Given this code, what is the output?
Python
class Animal: sound = "Generic" class Cat(Animal): pass class Dog(Animal): sound = "Bark" print(Animal.sound) print(Cat.sound) print(Dog.sound)
Attempts:
2 left
💡 Hint
Child classes inherit class attributes unless they override them.
✗ Incorrect
Cat inherits 'sound' from Animal, so it prints 'Generic'. Dog overrides 'sound' with 'Bark'.