How do you add a custom attribute named 'color' with value 'red' to an object 'car'?
Acar['color'] = 'red'
Bcar.color = 'red'
Ccar.add_attribute('color', 'red')
Dsetattr(car, 'color')
✗ Incorrect
You use dot notation to add a custom attribute: car.color = 'red'.
Which of these can you NOT add custom attributes to directly?
AUser-defined class instances
BObjects created from classes
CCustom classes
DBuilt-in types like int
✗ Incorrect
Built-in types like int do not allow adding custom attributes directly.
What happens if you add a custom attribute to a class?
AAll instances of the class get the attribute unless overridden
BThe attribute is deleted from instances
COnly that class instance gets the attribute
DIt raises an error
✗ Incorrect
Adding an attribute to a class makes it available to all instances unless they have their own attribute with the same name.
Which function can also add attributes dynamically to an object?
Adelattr()
Bgetattr()
Csetattr()
Dhasattr()
✗ Incorrect
setattr(object, name, value) adds or changes an attribute dynamically.
If you add an attribute to an instance, can it override a class attribute with the same name?
AYes, instance attribute overrides class attribute
BNo, class attribute always takes precedence
COnly if you delete the class attribute first
DIt causes an error
✗ Incorrect
Instance attributes override class attributes with the same name.
Explain how to add a custom attribute to a Python object and what happens when you add it to a class instead.
Think about the difference between instance and class level.
You got /4 concepts.
Describe why you cannot add custom attributes to built-in types like int or str in Python.
Consider how built-in types are implemented.
You got /3 concepts.
Practice
(1/5)
1. What does adding a custom attribute to a Python object allow you to do?
easy
A. Delete the object from memory
B. Change the object's type permanently
C. Store extra information directly on that object
D. Prevent the object from being used in functions
Solution
Step 1: Understand what attributes are
Attributes are values or properties stored inside an object to hold data or state.
Step 2: Adding custom attributes
When you add a custom attribute, you attach new data directly to that object, allowing it to hold extra information.
Final Answer:
Store extra information directly on that object -> Option C
Quick Check:
Custom attributes = extra data on object [OK]
Hint: Custom attributes add new data fields to objects [OK]
Common Mistakes:
Thinking attributes change the object's type
Confusing attributes with deleting objects
Believing attributes restrict object usage
2. Which of the following is the correct syntax to add a custom attribute color with value 'red' to an object car?
easy
A. car.color = 'red'
B. car['color'] = 'red'
C. car->color = 'red'
D. car.color('red')
Solution
Step 1: Identify attribute assignment syntax
In Python, attributes are assigned using dot notation: object.attribute = value.
Step 2: Check each option
car.color = 'red' uses dot notation correctly. car['color'] = 'red' uses dictionary syntax which is invalid for normal objects. car->color = 'red' uses arrow notation which is not Python syntax. car.color('red') tries to call attribute as a method, which is incorrect here.
Final Answer:
car.color = 'red' -> Option A
Quick Check:
Use dot notation to add attributes [OK]
Hint: Use dot notation: object.attribute = value [OK]
Common Mistakes:
Using dictionary syntax on objects
Using arrow (->) like in other languages
Calling attribute as a function
3. What will be the output of this code?
class Dog:
pass
my_dog = Dog()
my_dog.age = 5
print(my_dog.age)
medium
A. AttributeError
B. age
C. Dog
D. 5
Solution
Step 1: Understand attribute assignment
The code creates an empty class Dog and an instance my_dog. Then it adds a custom attribute age with value 5 to my_dog.
Step 2: Print the attribute value
Printing my_dog.age outputs the value stored, which is 5.
Final Answer:
5 -> Option D
Quick Check:
Custom attribute value prints correctly [OK]
Hint: Print attribute after assignment to see its value [OK]
Common Mistakes:
Expecting class name or attribute name as output
Thinking attribute does not exist yet
Confusing attribute with method call
4. Find the error in this code that tries to add a custom attribute height to an object person:
class Person:
def __init__(self, name):
self.name = name
person = Person('Alice')
person.height = 170
print(person['height'])
medium
A. Missing height parameter in __init__
B. Using person['height'] instead of person.height
C. Cannot add attributes after object creation
D. Name attribute should be height
Solution
Step 1: Check how attribute is added
The code correctly adds height using dot notation: person.height = 170.
Step 2: Identify incorrect attribute access
Printing uses person['height'], which is dictionary syntax and invalid for normal objects. It should be person.height.
Final Answer:
Using person['height'] instead of person.height -> Option B
Quick Check:
Access attributes with dot, not brackets [OK]
Hint: Use dot notation to access attributes, not brackets [OK]
Common Mistakes:
Using dictionary syntax on objects
Thinking attributes must be in __init__
Believing attributes can't be added later
5. You want to add a custom attribute status to multiple instances of a class Task only if their priority is above 5. Which code correctly does this?
hard
A. for t in tasks:
if t.priority > 5:
t.status = 'urgent'
B. for t in tasks:
t.status = 'urgent' if t.priority > 5 else None
C. for t in tasks:
if t.priority > 5:
t['status'] = 'urgent'
D. for t in tasks:
t.status = 'urgent'
if t.priority <= 5:
del t.status
Solution
Step 1: Understand the condition and attribute addition
We want to add status only if priority > 5, so we check this condition inside the loop.
Step 2: Check each option's logic and syntax
for t in tasks:
if t.priority > 5:
t.status = 'urgent' adds status only when priority > 5 using dot notation correctly. for t in tasks:
t.status = 'urgent' if t.priority > 5 else None assigns status to None for others, which adds the attribute unnecessarily. for t in tasks:
if t.priority > 5:
t['status'] = 'urgent' uses dictionary syntax which is invalid. for t in tasks:
t.status = 'urgent'
if t.priority <= 5:
del t.status adds status to all then deletes for low priority, which is inefficient and error-prone.
Final Answer:
for t in tasks:
if t.priority > 5:
t.status = 'urgent' -> Option A
Quick Check:
Use if condition and dot notation to add attributes selectively [OK]
Hint: Add attributes inside if-block with dot notation [OK]
Common Mistakes:
Using dictionary syntax on objects
Adding attribute to all instances regardless of condition