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 | Aacc = Account() print(acc._balance) | |
| 2def _calculate_fee(self): | BThe name has no leading underscore, so it is public. A protected attribute needs a single underscore: self._balance = 0. | |
| 3A subclass can use a protected attribute inherited from its parent class | CDefines a protected method that is intended to be called only by the class itself or its subclasses | |
| 4Protected is a convention, not enforcement: code outside the class can still access the attribute | DCode outside the class and its subclasses should not use a protected attribute directly. Use a public method or property such as acc.get_balance() instead. | |
| 5self.balance = 0 # meant to be protected | ECreates a protected attribute: the single underscore signals it is meant for use only inside the class and its subclasses | |
| 6print(acc._balance) # in main program | Fclass Savings(Account): def show(self): print(self._balance) |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
_name — A single underscore prefix marks a protected member: by convention it is for use inside the class and its subclasses only. Python does not enforce it, so outside code can still access it.class Pet:
_name = "Rex"
age = 3class Dog(Pet):
def speak(self):
return self._name + " barks"class Account:
def _check(self):
return self.balance > 0acc = Account(50)
print(acc._balance)
# Python allows thisclass Saver(Account):
def show(self):
print(self._check())