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.name = name | AThe attribute is accessed without an object. Outside the class a public attribute needs the object and a dot, like pet.name. | |
| 2def speak(self): | BDefines a public method that can be called from inside or outside the class | |
| 3Code outside the class can read a public attribute using the object name and a dot | CCreates a public attribute with no leading underscore, so any code can read or change it | |
| 4Code outside the class can call a public method on an object | DThe leading underscore makes the attribute protected by convention, not public. Use self.name to make it public. | |
| 5self._name = name # meant to be public | Epet.speak() | |
| 6print(name) # outside the class | Fprint(pet.name) |
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 public member has no leading underscore, so code inside or outside the class can access it, e.g. self.name in the class or pet.name outside it.pet = Pet("Milo")
pet.speak()
print(pet.name)pet = Pet("Milo")
print(pet.name)class Pet:
def speak(self):
print("Woof!") def intro(self):
self.speak()pet = Pet("Milo")
print(pet.name)
pet.speak()