Why object-oriented programming is used

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

Why object-oriented programming is used

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program stores student data in three parallel lists: names, ages and scores. What is the main risk?
A The lists can fall out of step, so index 2 no longer matches one student
B Python cannot store more than one list at a time
2. Which statement best describes object-oriented programming?
A It stores all data in one list and keeps all code in separate functions
B It groups related data and the code that uses it into one unit called an object
3. You are building a game with players, enemies and items. Why is OOP a good fit?
A OOP is the only way Python can run a game
B Each real-world thing can be modelled as an object with its own data and actions
4. A list of dictionaries holds pet data like {'name': 'Rex', 'age': 3}. What is a limit of this approach?
A The dictionaries hold data only, so the code that uses it lives separately in functions
B Dictionaries cannot hold a name and an age together
5. Which one is an example of data and behaviour together?
A A Dog object with a name attribute and a bark() method
B A list called dog_names and a separate list called dog_ages

Match the Pairs

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

Left columnRight column
1names = ['Ava', 'Liam'] scores = [88, 65] AWith parallel lists, removing an item from one list leaves the other lists out of step, so names and scores no longer match
2class Student: def __init__(self, name, score): BA class bundles the data (name, score) for one real-world thing into a single unit
3Data and behaviour live together: an object holds its own data and has methods that work on that data Cdef add_points(self, n): self.points += n
4Modelling a real-world thing: one object stands for one student, pet or game character Dava = Student('Ava', 88)
5names.remove('Ava') # scores still has Ava's mark EParallel lists: related data is split across separate lists, so every list must be kept in the same order by hand
6student = {'name': 'Ava', 'score': 88} student['scroe'] = 90 FA dictionary has no fixed structure or built-in behaviour, so a typo in a key silently creates a new key instead of giving an error

Definitions

object-oriented programming

Fill in the Blanks

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

1.
names = ["Ava", "Liam"] points = [120, 95] print(names[0], points[_])
2.
students = [{"name": "Ava", "pts": 120}] for s __ students: print(s)
3.
_____ Student: def show(self): print("Hi")
4.
class Dog: ___ bark(self): print("Woof")
5.
class Car: pass mine = ___()

Spot the Error

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

Question 1
A names = ["Ava", "Liam", "Mia"]
B marks = [88, 65, 95]
C print(names[1], marks[2])
Question 2
A names = ["Ava", "Liam"]
B marks = [88, 65]
C names.append("Mia")
D marks.add(95)
Question 3
A p = {"name": "Ava", "age": 15}
B p.age += 1
C print(p["name"], p["age"])
Question 4
A pupils = [
B {"name": "Ava" "xp": 88},
C {"name": "Liam", "xp": 65}
D ]
Question 5
A Class Pupil:
B def __init__(self, name):
C self.name = name
D def greet(self):
E print("Hi", self.name)

Answer Key

Why object-oriented programming is used
Section A: Quick Questions
Q1
A — The lists can fall out of step, so index 2 no longer matches one student
Q2
B — It groups related data and the code that uses it into one unit called an object
Q3
B — Each real-world thing can be modelled as an object with its own data and actions
Q4
A — The dictionaries hold data only, so the code that uses it lives separately in functions
Q5
A — A Dog object with a name attribute and a bark() method
Section B: Match the Pairs
Q1
names = ['Ava', 'Liam'] scores = [88, 65] → Parallel lists: related data is split across separate lists, so every list must be kept in the same order by hand
Q2
class Student: def __init__(self, name, score): → A class bundles the data (name, score) for one real-world thing into a single unit
Q3
Data and behaviour live together: an object holds its own data and has methods that work on that data → def add_points(self, n): self.points += n
Q4
Modelling a real-world thing: one object stands for one student, pet or game character → ava = Student('Ava', 88)
Q5
names.remove('Ava') # scores still has Ava's mark → With parallel lists, removing an item from one list leaves the other lists out of step, so names and scores no longer match
Q6
student = {'name': 'Ava', 'score': 88} student['scroe'] = 90 → A dictionary has no fixed structure or built-in behaviour, so a typo in a key silently creates a new key instead of giving an error
Section C: Definitions
Q1
object-oriented programming — A way of writing programs that groups related data and the code that uses it into one unit called an object, so programs modelling real things are easier to organise than separate lists and functions.
Section D: Fill in the Blanks
Q1
names = ["Ava", "Liam"] points = [120, 95] print(names[0], points[0])
Q2
students = [{"name": "Ava", "pts": 120}] for s in students: print(s)
Q3
class Student: def show(self): print("Hi")
Q4
class Dog: def bark(self): print("Woof")
Q5
class Car: pass mine = Car()
Section E: Spot the Error
Q1
C — Parallel lists only work if the same index is used in both lists. names[1] is Liam but marks[2] is Mia's mark. It should be marks[1]. This easy mistake is one reason parallel lists are risky.
Q2
D — Lists have no add() method, so this causes an AttributeError. Use marks.append(95). With parallel lists you must remember to update every list each time, or the data goes out of sync.
Q3
B — Dictionary values are reached with square brackets and a key, not dot notation. It should be p["age"] += 1. Dictionaries hold data, but they have no way to bundle behaviour with it.
Q4
B — A comma is missing between the key-value pairs "name": "Ava" and "xp": 88, which is a SyntaxError. It should be {"name": "Ava", "xp": 88},. A list of dictionaries stores the data, but nothing checks that every dictionary has the same keys.
Q5
A — The class keyword must be lowercase. It should be class Pupil:. A class groups data (name) and behaviour (greet) into one unit.