Choosing access modifiers for a class

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Choosing access modifiers for a class

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A BankAccount class has a deposit() method that other code must call to add money. Which access level should deposit() have?
A Public: deposit() is part of the interface other code uses
B Private: deposit() should be hidden from all other code
2. A BankAccount stores its balance. Other code should only change it through deposit() and withdraw(). Which is the best choice for the balance attribute?
A self.balance (public)
B self.__balance (private)
3. Which attribute name marks a member as protected in Python?
A _name
B __name
4. A Vehicle class has an attribute that its subclass Car needs to read and change, but outside code should not touch it. Which level fits best?
A Private (__speed)
B Protected (_speed)
5. Which statement about choosing access modifiers is correct?
A Expose only what other code needs and keep everything else hidden
B Make every member public so it is easy to reach from anywhere

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1self._balance = 0 AA protected attribute: one underscore tells other code it is meant for the class and its subclasses only
2self.__pin = 1234 Bdef __check_pin(self):
3Public for the interface: a method other code is meant to call is given a plain name CA private attribute: two underscores make it internal data that only this class should use
4Private for internal data: a helper that only the class itself needs is hidden with two underscores Ddef deposit(self, amount):
5self.__balance = 0 # a Savings subclass must read balance directly EA method that other code must call is marked private. It should be public (get_name) because it is part of the interface.
6def __get_name(self): return self.name # main code calls acc.__get_name() FBalance is private, so the subclass cannot easily use it. It should be protected (_balance) because subclasses need access.

Definitions

access modifier

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Account: def setup(self): self.__balance = 0
2.
class Pet: def setup(self): self._name = 'Rex'
3.
class Account: def deposit(self, amount): self.__balance += amount
4.
class Atm: def __check(self, pin): return pin == self.pin
5.
class Dog(Pet): def speak(self): print(self._name)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Wallet:
B _cash = 0
C def add(self, amt):
D self.__cash += amt
Question 2
A class Door:
B def __open(self):
C self.__locked = False
D d = Door()
E d.open()
Question 3
A class Animal:
B def __init__(self, name):
C self.__name = name
D class Dog(Animal):
E def show(self):
F print(self._name)
Question 4
A # Private names start with _
B class Safe:
C def __init__(self, code):
D self.__code = code
Question 5
A class Car:
B def __init__(self):
C self.__speed = 0
D self._fuel = 50
E def fuel_left(self):
F return self.__fuel

Answer Key

Choosing access modifiers for a class
Section A: Quick Questions
Q1
A — Public: deposit() is part of the interface other code uses
Q2
B — self.__balance (private)
Q3
A — _name
Q4
B — Protected (_speed)
Q5
A — Expose only what other code needs and keep everything else hidden
Section B: Match the Pairs
Q1
self._balance = 0 → A protected attribute: one underscore tells other code it is meant for the class and its subclasses only
Q2
self.__pin = 1234 → A private attribute: two underscores make it internal data that only this class should use
Q3
Public for the interface: a method other code is meant to call is given a plain name → def deposit(self, amount):
Q4
Private for internal data: a helper that only the class itself needs is hidden with two underscores → def __check_pin(self):
Q5
self.__balance = 0 # a Savings subclass must read balance directly → Balance is private, so the subclass cannot easily use it. It should be protected (_balance) because subclasses need access.
Q6
def __get_name(self): return self.name # main code calls acc.__get_name() → A method that other code must call is marked private. It should be public (get_name) because it is part of the interface.
Section C: Definitions
Q1
access modifier — Marks a class member as public (name), protected (_name) or private (__name), so the class exposes only what other code needs.
Section D: Fill in the Blanks
Q1
class Account: def setup(self): self.__balance = 0
Q2
class Pet: def setup(self): self._name = 'Rex'
Q3
class Account: def deposit(self, amount): self.__balance += amount
Q4
class Atm: def __check(self, pin): return pin == self.pin
Q5
class Dog(Pet): def speak(self): print(self._name)
Section E: Spot the Error
Q1
B — The cash is internal data, so it should be private with two underscores: __cash = 0. The single underscore makes it protected, and it no longer matches self.__cash in add().
Q2
B — open() is part of the public interface that other code calls, so it must have no underscores: def open(self). Making it private means d.open() fails.
Q3
C — The subclass Dog needs to read the name, so it should be protected with one underscore: self._name = name. A private __name is name-mangled and Dog cannot reach it.
Q4
A — Private members start with two underscores (__). A single underscore (_) marks a protected member.
Q5
D — The fuel level is internal data, so it should be private: self.__fuel = 50. With one underscore it is only protected and no longer matches self.__fuel in fuel_left().