Bird
Raised Fist0

Which of the following is the correct syntax to add a custom attribute color with value 'red' to an object car?

easy📝 Syntax Q12 of Q15
Python - Custom Exceptions
Which of the following is the correct syntax to add a custom attribute color with value 'red' to an object car?
Acar.color = 'red'
Bcar['color'] = 'red'
Ccar->color = 'red'
Dcar.color('red')
Step-by-Step Solution
Solution:
  1. Step 1: Identify attribute assignment syntax

    In Python, attributes are assigned using dot notation: object.attribute = value.
  2. 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.
  3. Final Answer:

    car.color = 'red' -> Option A
  4. Quick Check:

    Use dot notation to add attributes [OK]
Quick Trick: Use dot notation: object.attribute = value [OK]
Common Mistakes:
MISTAKES
  • Using dictionary syntax on objects
  • Using arrow (->) like in other languages
  • Calling attribute as a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes