Bird
0
0

Which of the following is the correct way to create a public attribute name inside a Python class constructor?

easy📝 Syntax Q12 of 15
Python - Encapsulation and Data Protection
Which of the following is the correct way to create a public attribute name inside a Python class constructor?
Aself->name = value
Bself.name = value
Cname = self.value
Ddef name(self):
Step-by-Step Solution
Solution:
  1. Step 1: Recall syntax for public attributes

    Inside __init__, public attributes are created by assigning to self.attribute_name.
  2. Step 2: Check each option

    self.name = value uses correct syntax: self.name = value. Others are invalid Python syntax or wrong usage.
  3. Final Answer:

    self.name = value -> Option B
  4. Quick Check:

    Use self.attribute = value [OK]
Quick Trick: Use self.attribute = value inside __init__ [OK]
Common Mistakes:
  • Using def instead of assignment
  • Wrong arrow syntax like self->name
  • Assigning attribute to name instead of self.name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes