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 public method changes the private data without checking that the amount is valid, so it does not protect the data. | |
| 2def deposit(self, amount): | Bif amount > 0: self._balance += amount | |
| 3Encapsulation protects data from invalid values by checking them inside a method before changing a private attribute | CA public method that forms part of the interface, letting outside code change the data in a controlled way | |
| 4Outside code should use the public interface rather than reaching into private data | DChanges the private attribute directly, bypassing the public method so no validation happens and an invalid negative balance is stored. | |
| 5account._balance = -500 | Eaccount.deposit(50) | |
| 6def deposit(self, amount): self._balance += amount | FCreates a private attribute by convention, signalling that outside code should not change the balance directly |
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 methods together and restricting access, so private data is only changed through public methods that can check values are valid.class Student:
def setup(self):
self._mark = 0class Student:
def getMark(self):
return self.markdef setMark(self, mark):
if 0 <= mark <= 100:
self.mark = markdef setAge(self, age):
if age >= 0:
self.age = agedef deposit(self, amount):
if amount > 0:
self.balance += amount