Bird
Raised Fist0

Which of the following is the correct syntax to define an instance attribute 'age' in a class?

easy📝 Syntax Q3 of Q15
Python - Classes and Object Lifecycle
Which of the following is the correct syntax to define an instance attribute 'age' in a class?
Aclass.age = age
Bage = self.age
Cself.age = age
Ddef age(self):
Step-by-Step Solution
Solution:
  1. Step 1: Understand attribute assignment syntax

    Instance attributes are assigned using self.attribute_name = value inside methods.
  2. Step 2: Check each option

    Only 'self.age = age' correctly assigns the value to the instance attribute.
  3. Final Answer:

    self.age = age -> Option C
  4. Quick Check:

    Assign instance attribute with self.attribute = value [OK]
Quick Trick: Use self.attribute = value to set instance attributes [OK]
Common Mistakes:
MISTAKES
  • Reversing assignment order
  • Using class name instead of self
  • Defining a method instead of attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes