OOP principle: abstraction

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

OOP principle: abstraction

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes abstraction in OOP?
A Including every possible detail about a real-world thing in a class
B Modelling only the attributes and methods that matter for the purpose
2. A school app has a Student class. Which attributes are the better abstraction?
A studentId, firstName, yearLevel, house
B shoeSize, favouriteColour, petName, bloodType
3. What does a method like car.start() show about abstraction?
A It hides complex detail (fuel, spark, engine) behind one simple method call
B It forces the user to write code for every engine part
4. A game and a bank both model a Person class. Which statement is true?
A Both must include exactly the same attributes, because it is the same real person
B Each can use a different abstraction of Person, because their purposes differ
5. A Library app models a Book. Which attribute is relevant to the purpose?
A isbn
B paperWeightInGrams

Match the Pairs

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

Left columnRight column
1class Student: def __init__(self, name, year): Aclass Member: def __init__(self, name, cardNo):
2account.deposit(50) BThe calling code repeats the detail of how the total is calculated instead of using a method like cart.calculate_total() that hides it.
3Abstraction: the same real thing (a person) can be modelled with different attributes for different purposes, such as a library app. Cdef calculate_total(self): return sum(self._prices)
4Abstraction: a method hides how a task is done so other code only needs to know what it does. DAbstraction: the Student class keeps only the attributes a school app needs (name, year level) and leaves out details like eye colour or favourite food.
5class Student: def __init__(self, name, eyeColour, pet, shoeSize, year): EAbstraction: the deposit() method hides the complex steps (checking, updating, logging) so the caller just asks for the result.
6total = 0 for p in cart._prices: total += p print(total) FThe class includes irrelevant attributes (eyeColour, pet, shoeSize) for a school app. Abstraction means keeping only the attributes that matter for the purpose, such as name and year.

Definitions

abstraction

Fill in the Blanks

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

1.
_____ Student: def show(self): return self.name
2.
class Car: ___ start(self): self.check()
3.
class Kettle: def boil(self): ____.heat()
4.
class Patient: def info(self): ______ self.allergy
5.
def area(w, h): return w * h print(____(3, 4))

Spot the Error

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

Question 1
A class Student:
B def __init__(self, name):
C self.name = name
D def show():
E print(self.name)
Question 2
A class Book:
B def _init_(self, title):
C self.title = title
D b = Book("Dune")
E print(b.title)
Question 3
A class Car:
B def start(self):
C print("Engine on"
D my_car = Car()
E my_car.start()
Question 4
A class BusStop:
B def __init__(self, name):
C name = name
D def show(self):
E print(self.name)
Question 5
A class City:
B def __init__(self, name):
C self.name = name
D self.roads == []
E c = City("Perth")

Answer Key

OOP principle: abstraction
Section A: Quick Questions
Q1
B — Modelling only the attributes and methods that matter for the purpose
Q2
A — studentId, firstName, yearLevel, house
Q3
A — It hides complex detail (fuel, spark, engine) behind one simple method call
Q4
B — Each can use a different abstraction of Person, because their purposes differ
Q5
A — isbn
Section B: Match the Pairs
Q1
class Student: def __init__(self, name, year): → Abstraction: the Student class keeps only the attributes a school app needs (name, year level) and leaves out details like eye colour or favourite food.
Q2
account.deposit(50) → Abstraction: the deposit() method hides the complex steps (checking, updating, logging) so the caller just asks for the result.
Q3
Abstraction: the same real thing (a person) can be modelled with different attributes for different purposes, such as a library app. → class Member: def __init__(self, name, cardNo):
Q4
Abstraction: a method hides how a task is done so other code only needs to know what it does. → def calculate_total(self): return sum(self._prices)
Q5
class Student: def __init__(self, name, eyeColour, pet, shoeSize, year): → The class includes irrelevant attributes (eyeColour, pet, shoeSize) for a school app. Abstraction means keeping only the attributes that matter for the purpose, such as name and year.
Q6
total = 0 for p in cart._prices: total += p print(total) → The calling code repeats the detail of how the total is calculated instead of using a method like cart.calculate_total() that hides it.
Section C: Definitions
Q1
abstraction — Modelling only the attributes and methods that matter for a purpose and hiding the rest of the detail, such as a Student class that keeps just the data a school app needs.
Section D: Fill in the Blanks
Q1
class Student: def show(self): return self.name
Q2
class Car: def start(self): self.check()
Q3
class Kettle: def boil(self): self.heat()
Q4
class Patient: def info(self): return self.allergy
Q5
def area(w, h): return w * h print(area(3, 4))
Section E: Spot the Error
Q1
D — Every method in a class needs self as its first parameter. The Student class keeps only the name a school app needs and hides the rest behind show(). It should be def show(self):
Q2
B — The constructor needs double underscores on both sides. The Book class models just the title a library search needs. It should be def __init__(self, title):
Q3
C — The print call is missing its closing bracket. A driver only calls start() and the engine detail stays hidden inside the method. It should be print("Engine on")
Q4
C — This only sets a local variable, so the object never stores the attribute. A bus app keeps just the stop name, so it should be self.name = name
Q5
D — A double equals compares values. It does not assign them. A map app models a city by its roads (a tourism app would keep different details). It should be self.roads = []