OOP – Practical Application

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

OOP – Practical Application

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why use OOP for a bank account?
A Groups related data and behavior together
B It's faster
2. What should __init__ set for a BankAccount?
A owner and balance
B Only the class name
3. Why check balance before withdrawing?
A Python requires it
B To prevent overdraft
4. acc.deposit(50) what should this do?
A Create a new account
B Add 50 to self.balance
5. Can one class have many objects?
A Yes each with their own data
B No only one

Match the Pairs

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

Left columnRight column
1def __init__(name): AError: balance is an attribute, not a method
2acc.balance() BOk: Update an instance attribute in a method
3def __init__(self, name): CValid: Constructor with self and a parameter
4self.balance += amount DOutput: 7
5round(3.14159, 2) EOutput: 3.14
6abs(-7) FError: Missing self in __init__

Definitions

Encapsulation

Fill in the Blanks

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

1.
class BankAccount: def deposit(self, amount): self._______ += amount
2.
class TodoList: def add(self, task): self.tasks.______(task)
3.
class BankAccount: def withdraw(self, amount): if amount <= self._______: self.balance -= amount
4.
acc = BankAccount('Alice', 100) acc._______(50)
5.
class Score: def ________(self): self.points = _

Spot the Error

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

Question 1
A class BankAccount:
B def withdraw(self, amount):
C self.balance -= amount
Question 2
A class BankAccount:
B def deposit(self, amount):
C balance += amount
Question 3
A class TodoList:
B def __init__(self):
C tasks = []
D def add(self, task):
E self.tasks.append(task)
Question 4
A class BankAccount:
B def deposit(amount):
C self.balance += amount
Question 5
A class BankAccount:
B def __init__(self):
C self.balance = 0
D def deposit(self, amount):
E self.balance += amount

Answer Key

OOP – Practical Application
Section A: Quick Questions
Q1
A — Groups related data and behavior together
Q2
A — owner and balance
Q3
B — To prevent overdraft
Q4
B — Add 50 to self.balance
Q5
A — Yes each with their own data
Section B: Match the Pairs
Q1
def __init__(name): → Error: Missing self in __init__
Q2
acc.balance() → Error: balance is an attribute, not a method
Q3
def __init__(self, name): → Valid: Constructor with self and a parameter
Q4
self.balance += amount → Ok: Update an instance attribute in a method
Q5
round(3.14159, 2) → Output: 3.14
Q6
abs(-7) → Output: 7
Section C: Definitions
Q1
Encapsulation — Bundling data and methods that operate on it together in a class.~~State
Section D: Fill in the Blanks
Q1
class BankAccount: def deposit(self, amount): self.balance += amount
Q2
class TodoList: def add(self, task): self.tasks.append(task)
Q3
class BankAccount: def withdraw(self, amount): if amount <= self.balance: self.balance -= amount
Q4
acc = BankAccount('Alice', 100) acc.deposit(50)
Q5
class Score: def __init__(self): self.points = 0
Section E: Spot the Error
Q1
C — No balance check could go negative. Add validation.
Q2
C — Must use self.balance balance alone is undefined here
Q3
C — tasks = [] is a local variable. Use self.tasks = [] to store on object.
Q4
B — Missing self as first parameter
Q5
E — No validation negative deposits shouldn't be allowed