Encapsulation — @property, Underscore Conventions & Name Mangling — Concept Check
Questions on Encapsulation — @property, Underscore Conventions & Name Mangling. Python replaces getters/setters with @property — attribute syntax with method control — and replaces private keywords with conventions: _protected and __mangled.
Questions in this quiz
8 questions on Encapsulation — @property, Underscore Conventions & Name Mangling — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.An engineer wants to prevent accidental modification of a class attribute from outside the class. Which of the following should they use?
- AA private keyword
- BThe @property decorator
- CA double leading underscore before the attribute name
- DA single leading underscore before the attribute name
2.A developer is working with a Python class and notices that an attribute is accessed using method syntax but behaves like a property. What Python feature is being used?
- ASingle underscore convention
- BName mangling
- CPrivate keyword
- DThe @property decorator
3.In a Python class, an attribute with a double underscore prefix is not accessible directly. What is the reason for this behavior?
- AThe attribute name is mangled to prevent accidental access
- BThe attribute is truly private and inaccessible
- CThe attribute is protected by the @property decorator
- DThe attribute is read-only
4.A developer has a class with an attribute defined using the @property decorator but without a setter. What will happen if they try to set a value to this attribute?
- AAn AttributeError will be raised
- BA ValueError will be raised
- CThe attribute will be set to None
- DThe value will be set successfully
5.Consider a class with an attribute defined using a single underscore prefix. What does this signify?
- AThe attribute is private and cannot be accessed
- BThe attribute is public and can be accessed freely
- CThe attribute is intended for internal use only
- DThe attribute is protected by the @property decorator
6.A Python class has an attribute with a double underscore prefix. How can a subclass safely define an attribute with the same name?
- ABy using a single underscore prefix in the subclass
- BBy using a different attribute name entirely
- CBy relying on name mangling to handle it
- DBy using the same double underscore prefix in the subclass
7.What is the primary advantage of using the @property decorator in a Python class?
- AIt prevents attribute access from outside the class
- BIt enables attribute name mangling
- CIt allows for computed and validated attributes
- DIt makes attributes private
8.A developer attempts to access a mangled attribute directly using its original name and encounters an AttributeError. What should they do to access it?
- AAccess it using the mangled name
- BUse the @property decorator
- CUse a single underscore instead
- DRemove the double underscore