Bird
0
0

Which of the following is the correct way to define a protected attribute named _count inside a Python class?

easy📝 Syntax Q12 of 15
Python - Encapsulation and Data Protection
Which of the following is the correct way to define a protected attribute named _count inside a Python class?
Aself._count = 0
Bcount = 0
Cself.__count = 0
Dself.count = 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify protected attribute syntax

    Protected attributes start with a single underscore, so self._count is correct.
  2. Step 2: Check other options

    self.count is public, self.__count is private, and count = 0 is a local variable, not an attribute.
  3. Final Answer:

    self._count = 0 -> Option A
  4. Quick Check:

    Protected attribute = single underscore prefix [OK]
Quick Trick: Use single underscore for protected attributes inside classes [OK]
Common Mistakes:
  • Using no underscore for protected attribute
  • Using double underscore for protected instead of private
  • Defining attribute without self inside methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes