0
0
Pythonprogramming~20 mins

Public attributes in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Public Attributes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AAttributeError
Bcar.brand
CNone
DToyota
Attempts:
2 left
💡 Hint
Public attributes can be accessed directly using the object name.
Predict Output
intermediate
2: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)
AAlice
BNone
CBob
DAttributeError
Attempts:
2 left
💡 Hint
Public attributes can be changed directly.
Predict Output
advanced
2: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)
APython 101
BNone
CAttributeError
Dbook.title
Attempts:
2 left
💡 Hint
Public attributes are accessible from outside the class.
Predict Output
advanced
2: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)
AAttributeError
BSyntaxError
CNone
DDog
Attempts:
2 left
💡 Hint
Deleting a public attribute removes it from the object.
🧠 Conceptual
expert
2: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)
ANone
B20
C10
DAttributeError
Attempts:
2 left
💡 Hint
Child class overrides the public attribute after calling the parent constructor.