Abstraction in OOP

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

Abstraction in OOP

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes abstraction in OOP?
A A class shows every detail of how it works so users can change anything
B A class shows only what is needed to use it and hides the complicated working
2. You call account.deposit(50) without knowing how the balance is updated inside. What is this an example of?
A Abstraction: using a method without knowing how it works
B A syntax error because you must know how deposit() works
3. You are designing a Student class for a school library system. Which attributes are best to model?
A name, student_id and books_borrowed
B name, favourite_colour, shoe_size and pet_name
4. Which class design shows better abstraction?
A A Kettle class where the user must call 10 separate methods to boil water
B A Kettle class with a simple boil() method that hides the heating steps
5. True or false: Abstraction means the complicated code does not exist.
A False: the code exists but is hidden inside the class
B True: abstraction removes the complicated code completely

Match the Pairs

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

Left columnRight column
1account.deposit(50) AThe code reaches into the class and does the working itself instead of calling a simple method like cart.get_total().
2class Student: def __init__(self, name, year): Bclass Book: def __init__(self, title, author):
3Abstraction lets you use a method without knowing how it works inside CModels only the details needed (name and year) and leaves out everything else about a real student
4A good class design only includes attributes the program actually needs Dprint(car.get_speed())
5class Player: def __init__(self, name, shoe_size, eye_colour, lucky_number): EToo many unneeded attributes. Abstraction means only modelling the details the program needs, such as name and score.
6total = 0 for item in cart.items: total = total + item.price FUses a simple method to add money without needing to know how the balance is updated inside the class

Definitions

abstraction

Fill in the Blanks

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

1.
_____ BankAccount: def deposit(self, amount): self.balance += amount
2.
account = BankAccount(100) account._______(50) print(account.balance)
3.
def deposit(self, amount): self.balance __ amount
4.
def area(self): ______ self.width * self.height
5.
class Thermostat: ___ heat(self, temp): self.temp = temp

Spot the Error

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

Question 1
A class Kettle:
B def __init__(self):
C self.temp = 20
D def boil():
E self.temp = 100
Question 2
A class Car:
B def __init__(self, fuel):
C fuel = self.fuel
D def drive(self):
E self.fuel -= 1
Question 3
A class Phone:
B def __init__(self)
C self.battery = 100
D def charge(self):
E self.battery = 100
Question 4
A class Calculator
B def square(self, n):
C return n * n
Question 5
A class Toaster:
B def toast(self):
C return 'Toast is ready'
D t = Toaster
E print(t.toast())

Answer Key

Abstraction in OOP
Section A: Quick Questions
Q1
B — A class shows only what is needed to use it and hides the complicated working
Q2
A — Abstraction: using a method without knowing how it works
Q3
A — name, student_id and books_borrowed
Q4
B — A Kettle class with a simple boil() method that hides the heating steps
Q5
A — False: the code exists but is hidden inside the class
Section B: Match the Pairs
Q1
account.deposit(50) → Uses a simple method to add money without needing to know how the balance is updated inside the class
Q2
class Student: def __init__(self, name, year): → Models only the details needed (name and year) and leaves out everything else about a real student
Q3
Abstraction lets you use a method without knowing how it works inside → print(car.get_speed())
Q4
A good class design only includes attributes the program actually needs → class Book: def __init__(self, title, author):
Q5
class Player: def __init__(self, name, shoe_size, eye_colour, lucky_number): → Too many unneeded attributes. Abstraction means only modelling the details the program needs, such as name and score.
Q6
total = 0 for item in cart.items: total = total + item.price → The code reaches into the class and does the working itself instead of calling a simple method like cart.get_total().
Section C: Definitions
Q1
abstraction — A class shows only the details needed to use it and hides the complicated working behind simple methods, so you can use a method without knowing how it works inside.
Section D: Fill in the Blanks
Q1
class BankAccount: def deposit(self, amount): self.balance += amount
Q2
account = BankAccount(100) account.deposit(50) print(account.balance)
Q3
def deposit(self, amount): self.balance += amount
Q4
def area(self): return self.width * self.height
Q5
class Thermostat: def heat(self, temp): self.temp = temp
Section E: Spot the Error
Q1
D — A method needs self as its first parameter so it can reach the object's data. The simple boil() method hides the working, so it must be def boil(self):.
Q2
C — The value passed in must be stored in the object with self.fuel = fuel. The broken line reads the attribute before it exists, so the model of the car has no fuel.
Q3
B — The def line is missing its colon. It should be def __init__(self): so the constructor sets up only the details the Phone model needs.
Q4
A — A class header must end with a colon: class Calculator:. Users can then call square() without knowing how it works inside.
Q5
D — Brackets are needed to create an object: t = Toaster(). Without them t is the class itself, so t.toast() will not work as a simple method call.