Encapsulation Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
What is the purpose of using properties in Python classes?
  • A To use getter/setter with attribute syntax
  • B To create read-only attributes
  • C To improve performance
  • D To enable multiple inheritance
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?
  • A Define only getter without setter
  • B Use double underscore prefix
  • C Use the "readonly" keyword
  • D Make attribute name uppercase
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?
  • A Restricting direct access to internal data
  • B Encrypting data in memory
  • C Storing data in hidden files
  • D Deleting sensitive data
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?
  • A Abstraction
  • B Inheritance
  • C Polymorphism
  • D Recursion
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?
  • A AttributeError occurs due to name mangling
  • B It works normally
  • C SyntaxError occurs
  • D ValueError is raised
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?
  • A Using setter methods with validation
  • B Using constructor only
  • C Using try-except blocks everywhere
  • D Using assert statements in main
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?
  • A Single underscore prefix (_member)
  • B Double underscore prefix (__member)
  • C No special prefix
  • D Uppercase names
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?
  • A Prevents unintended interference between components
  • B Makes code run faster
  • C Reduces memory usage
  • D Automatically fixes bugs
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?
  • A __private uses name mangling, _protected does not
  • B _protected is faster than __private
  • C __private cannot be inherited
  • D _protected requires special imports
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?
  • A Using @property.deleter decorator
  • B Using del keyword directly
  • C Using remove_property() function
  • D Using __delete__ method
Answer: Option A
Explanation: The @property.deleter decorator defines a deleter method that is called when del is used on the property.
Questions and Answers for Competitive Exams Various Entrance Test