Encapsulation Questions and Answers
Practice ModeShowing 10 of 25 questions
Q11
What is the purpose of using properties in Python classes?
Answer: Option A
Explanation: Properties allow you to use getter and setter methods while maintaining the attribute-like access syntax.
Q12
How do you make an attribute read-only using properties?
Answer: Option A
Explanation: To make an attribute read-only, define only the getter method with @property without defining a setter.
Q13
What is data hiding in encapsulation?
Answer: Option A
Explanation: Data hiding means restricting direct access to internal data and providing controlled access through methods.
Q14
Which principle is closely related to encapsulation?
Answer: Option A
Explanation: Abstraction and encapsulation are closely related concepts in OOP that work together to hide implementation details.
Q15
What happens when you try to access __private_var directly from outside?
Answer: Option A
Explanation: Python performs name mangling on double underscore attributes, making them accessible as _ClassName__private_var.
Q16
How can you validate data before setting an attribute value?
Answer: Option A
Explanation: Setter methods with validation logic allow you to check data before assigning it to attributes.
Q17
What is the convention for protected members in Python?
Answer: Option A
Explanation: Single underscore prefix is the convention for protected members, indicating they are for internal use.
Q18
Why is encapsulation important in large projects?
Answer: Option A
Explanation: Encapsulation prevents unintended interference and makes code more maintainable in large codebases.
Q19
What is the difference between _protected and __private in Python?
Answer: Option A
Explanation: Protected members are accessible but indicate internal use, while private members undergo name mangling.
Q20
How do you delete a property in Python?
Answer: Option A
Explanation: The @property.deleter decorator defines a deleter method that is called when del is used on the property.