05.02 Encapsulation And State - Quiz¶
Check your understanding
-
What is encapsulation in object-oriented programming?
- Hiding all methods from external access
- Bundling data and methods together while controlling access to internal details { data-correct }
- Making all attributes public
- Converting objects to strings
-
Which Python naming convention indicates a protected attribute?
attribute_attribute{ data-correct }__attributeATTRIBUTE
-
What is an invariant in object-oriented programming?
- A method that never changes
- A variable that cannot be modified
- A condition that must always be true for an object to be in a valid state { data-correct }
- A type of loop
-
Look at this code. What’s the problem with direct attribute access?
class BankAccount: def __init__(self, balance): self._balance = balance account = BankAccount(100) account._balance = -500 # Problem here- It’s perfectly fine
- It bypasses validation and can create invalid state { data-correct }
- It’s too slow
- It uses too much memory
-
What is the purpose of a getter method?
- To delete an attribute
- To provide controlled access to internal data { data-correct }
- To create new objects
- To sort data
-
When should you use a setter method?
- When you need to validate input before storing it { data-correct }
- When you want to make data public
- When you need to delete an object
- When you want to slow down your program
-
What does this Python property decorator do?
- Makes the method private
- Allows the method to be accessed like an attribute { data-correct }
- Deletes the attribute
- Makes the method static
-
Which of these demonstrates good encapsulation?
student.name = "John"student._grades[0] = 100student.add_grade(85){ data-correct }student.__secret = "hidden"
-
What happens if you violate an object’s invariants?
- The program runs faster
- The object may be in an invalid or inconsistent state { data-correct }
- Nothing changes
- The object becomes more secure
-
Which statement about public vs private interfaces is correct?
- All methods should be private
- The public interface should be stable while internal implementation can change { data-correct }
- Private methods are faster than public methods
- Public methods are automatically validated