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 | ACreates a private attribute; the double underscore makes Python name-mangle it so outside code cannot reach it directly | |
| 2def __log(self): | Bprint(acc.__balance) | |
| 3Reading a private attribute from outside the class raises an AttributeError | CCode outside the class cannot update the private attribute by its plain name. Python creates a new separate attribute instead. Use a method inside the class to change the private value. | |
| 4Python name-mangles a private name by changing __balance into _ClassName__balance behind the scenes | DOnly one underscore is used, so this is just a protected-by-convention name. Python does not mangle it and outside code can still read it. Use two underscores: self.__balance. | |
| 5self._balance = 0 | EDefines a private method that is meant to be called only from inside its own class | |
| 6acc.__balance = 50 # outside class | Facc._BankAccount__balance |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
self.__name — A private attribute: the double underscore makes Python name-mangle it, so code outside the class cannot reach it directly and gets an AttributeError.self.__balance = balancedef __verify(self, pin):acc = Account(50)
# Outside the class: AttributeError
print(acc.__balance)acc = Account(50)
print(acc._Account__balance)def show(self):
print(self.__balance)