Bird
0
0

To ensure a user's password attribute in a Python class cannot be modified directly, which encapsulation technique is most appropriate?

hard📝 Application Q8 of 15
Python - Encapsulation and Data Protection
To ensure a user's password attribute in a Python class cannot be modified directly, which encapsulation technique is most appropriate?
AUse a global variable to store the password
BMake the password attribute public and document not to change it
CDefine the password as a private attribute and provide only a getter method
DStore the password in a class variable accessible by all instances
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation for protection

    Making an attribute private prevents direct external modification.
  2. Step 2: Provide controlled access

    Providing only a getter method allows read-only access, preventing changes.
  3. Step 3: Analyze options

    Define the password as a private attribute and provide only a getter method correctly uses private attribute with getter only. Options B, C, and D do not protect the password.
  4. Final Answer:

    Define the password as a private attribute and provide only a getter method -> Option C
  5. Quick Check:

    Private attribute + getter = read-only access [OK]
Quick Trick: Private attribute with getter prevents direct modification [OK]
Common Mistakes:
  • Making password public and trusting users
  • Using global variables for sensitive data
  • Allowing direct write access to password attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes