Encapsulation Questions and Answers
Python Programming - Encapsulation Questions with Answers focus on data protection and abstraction concepts. These Python Encapsulation Questions are useful for Cognizant, Infosys, and Bank PO exams, helping learners understand how to restrict access to object data securely
Study encapsulation in object-oriented programming. Complement this with C# inheritance and polymorphism
Encapsulation
Showing 10 of
25 questions
1. What is encapsulation in Python?
- Bundling data and methods together within a class
- Converting code into machine language
- Breaking down large programs into smaller functions
- Connecting to external databases
2. Which access modifier in Python indicates a protected member?
- Single underscore (_)
- Double underscore (__)
- No underscore
- Triple underscore (___)
3. What is the purpose of name mangling in Python?
- To make attributes harder to access directly from outside
- To improve code execution speed
- To convert variable names to uppercase
- To allow multiple variables with same name
4. How do you create a private attribute in Python?
- Using double underscore prefix (__attribute)
- Using single underscore prefix (_attribute)
- Using the "private" keyword
- Using the "hidden" keyword
5. What are getter and setter methods used for in encapsulation?
- To provide controlled access to private attributes
- To make code run faster
- To connect to databases
- To create multiple class instances
6. Which Python decorator is commonly used for getter methods?
- @property
- @getter
- @attribute
- @access
7. What is the main benefit of encapsulation?
- Data protection and integrity
- Faster code execution
- Smaller file size
- Automatic documentation
8. How can you access a private attribute named __salary from outside the class?
- Using _ClassName__salary
- Using __salary directly
- Using salary without underscores
- Using getSalary() method only
9. What does the @attribute.setter decorator do?
- Defines a setter method for a property
- Creates a new attribute
- Deletes an attribute
- Makes attribute read-only
10. Which of the following is true about Python encapsulation?
- It is more about convention than strict enforcement
- It completely prevents access to private members
- It requires special compiler flags
- It only works in Python 3.8+