Encapsulation Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
What is encapsulation in Python?
  • A Bundling data and methods together within a class
  • B Converting code into machine language
  • C Breaking down large programs into smaller functions
  • D Connecting to external databases
Answer: Option A
Explanation: Encapsulation is one of the fundamental principles of object-oriented programming that bundles data and methods together while restricting direct access to some components.
Q2
Which access modifier in Python indicates a protected member?
  • A Single underscore (_)
  • B Double underscore (__)
  • C No underscore
  • D Triple underscore (___)
Answer: Option A
Explanation: In Python, single underscore (_) prefix indicates protected members, meaning they should not be accessed directly from outside the class.
Q3
What is the purpose of name mangling in Python?
  • A To make attributes harder to access directly from outside
  • B To improve code execution speed
  • C To convert variable names to uppercase
  • D To allow multiple variables with same name
Answer: Option A
Explanation: Name mangling with double underscore makes attributes harder to access directly from outside the class, providing a weak form of data hiding.
Q4
How do you create a private attribute in Python?
  • A Using double underscore prefix (__attribute)
  • B Using single underscore prefix (_attribute)
  • C Using the "private" keyword
  • D Using the "hidden" keyword
Answer: Option A
Explanation: Private attributes in Python are created using double underscore prefix, which triggers name mangling.
Q5
What are getter and setter methods used for in encapsulation?
  • A To provide controlled access to private attributes
  • B To make code run faster
  • C To connect to databases
  • D To create multiple class instances
Answer: Option A
Explanation: Getter and setter methods provide controlled access to private attributes, allowing validation and maintaining data integrity.
Q6
Which Python decorator is commonly used for getter methods?
  • A @property
  • B @getter
  • C @attribute
  • D @access
Answer: Option A
Explanation: The @property decorator is used to define getter methods that can be accessed like attributes.
Q7
What is the main benefit of encapsulation?
  • A Data protection and integrity
  • B Faster code execution
  • C Smaller file size
  • D Automatic documentation
Answer: Option A
Explanation: Encapsulation protects data integrity by preventing unauthorized access and modification of internal object state.
Q8
How can you access a private attribute named __salary from outside the class?
  • A Using _ClassName__salary
  • B Using __salary directly
  • C Using salary without underscores
  • D Using getSalary() method only
Answer: Option A
Explanation: Private attributes can still be accessed using name mangling format: _ClassName__attributeName
Q9
What does the @attribute.setter decorator do?
  • A Defines a setter method for a property
  • B Creates a new attribute
  • C Deletes an attribute
  • D Makes attribute read-only
Answer: Option A
Explanation: The @attribute.setter decorator defines a setter method for a property, allowing controlled modification of the attribute.
Q10
Which of the following is true about Python encapsulation?
  • A It is more about convention than strict enforcement
  • B It completely prevents access to private members
  • C It requires special compiler flags
  • D It only works in Python 3.8+
Answer: Option A
Explanation: Python encapsulation is more about convention than enforcement, as private members can still be accessed with name mangling.
Questions and Answers for Competitive Exams Various Entrance Test