Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1self._balance = 0 | AThe data is in the class but the deposit function is outside it, so data and methods are not bundled together | |
| 2def deposit(self, amount): | BA method in the same class that works on the object's data, keeping data and behaviour bundled together | |
| 3Protecting data: a method only accepts a new value if it passes a check, so the data cannot be set to something invalid | CStores data inside the object with a leading underscore to show it is protected and should not be changed directly from outside | |
| 4Benefit for maintenance: code outside the class uses a method, so the inside of the class can change without breaking other code | Dif amount > 0: self._balance += amount | |
| 5account._balance = -500 | Eaccount.get_balance() | |
| 6class Account: balance = 0 def deposit(acc, amount): | FChanges the protected attribute directly from outside the class, skipping the method that checks the value, so it breaks encapsulation |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
encapsulation — Bundling an object's data and the methods that use it into one class, and controlling access so the data is only changed in safe ways through those methods.class Account:
def __init__(self, balance):
self._balance = balance def balance(self):
return self._balance def deposit(self, amount):
if amount > 0:
self._balance += amount def withdraw(self, amount):
if amount <= self._balance:
self._balance -= amountacc = Account(100)
acc.deposit(50)
print(acc.get_balance())