Challenge - 5 Problems
Public Attributes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing a public attribute
What is the output of this code?
Python
class Car: def __init__(self, brand): self.brand = brand my_car = Car('Toyota') print(my_car.brand)
Attempts:
2 left
💡 Hint
Public attributes can be accessed directly using the object name.
✗ Incorrect
The attribute 'brand' is public and set in __init__. Accessing my_car.brand prints 'Toyota'.
❓ Predict Output
intermediate2:00remaining
Changing a public attribute value
What will be printed after changing the public attribute?
Python
class Person: def __init__(self, name): self.name = name p = Person('Alice') p.name = 'Bob' print(p.name)
Attempts:
2 left
💡 Hint
Public attributes can be changed directly.
✗ Incorrect
The attribute 'name' is changed from 'Alice' to 'Bob'. So printing p.name outputs 'Bob'.
❓ Predict Output
advanced2:00remaining
Accessing a public attribute from outside the class
What is the output of this code?
Python
class Book: def __init__(self, title): self.title = title b = Book('Python 101') print(b.title)
Attempts:
2 left
💡 Hint
Public attributes are accessible from outside the class.
✗ Incorrect
The attribute 'title' is public and can be accessed directly using the object b.
❓ Predict Output
advanced2:00remaining
Effect of deleting a public attribute
What happens when you run this code?
Python
class Animal: def __init__(self, species): self.species = species pet = Animal('Dog') del pet.species print(pet.species)
Attempts:
2 left
💡 Hint
Deleting a public attribute removes it from the object.
✗ Incorrect
After deleting pet.species, accessing pet.species raises AttributeError because it no longer exists.
🧠 Conceptual
expert2:00remaining
Understanding public attribute behavior in inheritance
Given this code, what is the output?
Python
class Parent: def __init__(self): self.value = 10 class Child(Parent): def __init__(self): super().__init__() self.value = 20 obj = Child() print(obj.value)
Attempts:
2 left
💡 Hint
Child class overrides the public attribute after calling the parent constructor.
✗ Incorrect
The Child class sets self.value to 20 after calling Parent's __init__, so obj.value is 20.