OOP principle: encapsulation

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

OOP principle: encapsulation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes encapsulation?
A Bundling an object's data and methods together and controlling access to the data
B Storing every variable in one global list so any code can reach it
2. Which attribute name signals a private attribute in Python?
A self.balance
B self._balance
3. How should outside code change a private attribute?
A Call a public method such as deposit(50)
B Set it directly, e.g. account._balance = -999
4. Why does encapsulation protect data from invalid values?
A Python automatically rejects any value that is too large
B A public method can check a value before the attribute is changed
5. Which setter method uses encapsulation correctly?
A if age >= 0: self._age = age
B self._age = age (no check at all)

Match the Pairs

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

Left columnRight 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

Definitions

encapsulation

Fill in the Blanks

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

1.
class Student: def setup(self): self._mark = 0
2.
class Student: def getMark(self): ______ self.mark
3.
def setMark(self, mark): __ 0 <= mark <= 100: self.mark = mark
4.
def setAge(self, age): if age __ 0: self.age = age
5.
def deposit(self, amount): if amount > 0: self.balance __ amount

Spot the Error

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

Question 1
A class Account:
B def __init__(self):
C _balance = 0
D def deposit(self, amt):
E if amt > 0:
F self._balance += amt
Question 2
A class Student:
B def __init__(self):
C self._mark = 0
D def set_mark(m):
E if 0 <= m <= 100:
F self._mark = m
Question 3
A class Player
B def __init__(self):
C self.__health = 100
D def get_health(self):
E return self.__health
Question 4
A class Car:
B def __init__(self)
C self._speed = 0
D def get_speed(self):
E return self._speed
Question 5
A Class Thermostat:
B def __init__(self):
C self._temp = 20
D def set_temp(self, t):
E if 10 <= t <= 30:
F self._temp = t

Answer Key

OOP principle: encapsulation
Section A: Quick Questions
Q1
A — Bundling an object's data and methods together and controlling access to the data
Q2
B — self._balance
Q3
A — Call a public method such as deposit(50)
Q4
B — A public method can check a value before the attribute is changed
Q5
A — if age >= 0: self._age = age
Section B: Match the Pairs
Q1
self._balance = 0 → Creates a private attribute by convention, signalling that outside code should not change the balance directly
Q2
def deposit(self, amount): → A public method that forms part of the interface, letting outside code change the data in a controlled way
Q3
Encapsulation protects data from invalid values by checking them inside a method before changing a private attribute → if amount > 0: self._balance += amount
Q4
Outside code should use the public interface rather than reaching into private data → account.deposit(50)
Q5
account._balance = -500 → Changes the private attribute directly, bypassing the public method so no validation happens and an invalid negative balance is stored.
Q6
def deposit(self, amount): self._balance += amount → The public method changes the private data without checking that the amount is valid, so it does not protect the data.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
class Student: def setup(self): self._mark = 0
Q2
class Student: def getMark(self): return self.mark
Q3
def setMark(self, mark): if 0 <= mark <= 100: self.mark = mark
Q4
def setAge(self, age): if age >= 0: self.age = age
Q5
def deposit(self, amount): if amount > 0: self.balance += amount
Section E: Spot the Error
Q1
C — Without self. this makes a local variable that disappears when __init__ ends. The data is not stored in the object. It should be self._balance = 0.
Q2
D — Every method in a class needs self as its first parameter so it can reach the object's private data. It should be def set_mark(self, m):.
Q3
A — A class header must end with a colon. It should be class Player:.
Q4
B — A method header must end with a colon. It should be def __init__(self):.
Q5
A — Python keywords are case sensitive. The keyword must be lowercase: class Thermostat:.