Creating several objects from one class

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

This Creating several objects from one class worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (Python) journey. Print it as a booklet?

Creating several objects from one class

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. After s1 = Student('Ava') and s2 = Student('Liam'), how many Student objects exist?
A Two separate objects, made from one class
B One object that both s1 and s2 share
2. Which code creates two objects from the same Dog class?
A class d1(Dog) class d2(Dog)
B d1 = Dog('Rex') d2 = Dog('Bella')
3. s1.name is 'Ava' and s2.name is 'Liam'. You run s2.name = 'Zoe'. What is s1.name now?
A 'Ava' - it did not change
B 'Zoe' - both objects changed
4. Two Player objects both have an add_points() method. Where is that method defined?
A Once in the class, shared by both objects
B Separately inside each object, written twice
5. p1 = Player('Mia') and p2 = Player('Noah'), both starting with score 0. After p1.add_points(10), what is p2.score?
A 10
B 0

Match the Pairs

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

Left columnRight column
1s2 = Student('Liam') AUsing the class name changes the attribute on the class, not on one object. To change only one student use the object name, like s1.name = 'Zoe'.
2s1.name = 'Zoe' Bs2 = s1 does not create a new object. It gives the same object a second name, so changing s2 also changes s1. Fix: s2 = Student('Liam').
3Two objects from the same class can hold different data in their attributes Cs1 = Student('Ava') s2 = Student('Liam')
4Objects from one class share the same methods but give different results because their data differs DCreates a second object from the Student class with its own separate name value
5s1 = Student('Ava') s2 = s1 EChanges the name of s1 only, so s2 keeps its old name
6Student.name = 'Zoe' Fs1.greet() s2.greet()

Definitions

s2 = Student('Liam')

Fill in the Blanks

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

1.
s1 = Student('Ava') s2 = _______('Liam')
2.
s1 = Student('Ava') s2 = Student('Liam') print(__.name) # Liam
3.
s1 = Student('Ava') s2 = Student('Liam') __.points = 5 # s1 is unchanged
4.
s1.greet() # Hi, I am Ava s2._____() # Hi, I am Liam
5.
class Student: def ________(self, name): self.name = name

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 s1 = Student('Ava')
E s2 = Student('Liam')
Question 2
A class Dog:
B def bark(self):
C print('Woof!)
D d1 = Dog()
E d2 = Dog()
F d2.bark()
Question 3
A class Car:
B def __init__(self, colour):
C self.colour = colour
D car1 = car('red')
E car2 = Car('blue')
Question 4
A Class Pet:
B def __init__(self, name):
C self.name = name
D p1 = Pet('Milo')
E p2 = Pet('Zara')
Question 5
A class Player
B def __init__(self, score):
C self.score = score
D a = Player(0)
E b = Player(0)

Answer Key

Creating several objects from one class
Section A: Quick Questions
Q1
A — Two separate objects, made from one class
Q2
B — d1 = Dog('Rex') d2 = Dog('Bella')
Q3
A — 'Ava' - it did not change
Q4
A — Once in the class, shared by both objects
Q5
B — 0
Section B: Match the Pairs
Q1
s2 = Student('Liam') → Creates a second object from the Student class with its own separate name value
Q2
s1.name = 'Zoe' → Changes the name of s1 only, so s2 keeps its old name
Q3
Two objects from the same class can hold different data in their attributes → s1 = Student('Ava') s2 = Student('Liam')
Q4
Objects from one class share the same methods but give different results because their data differs → s1.greet() s2.greet()
Q5
s1 = Student('Ava') s2 = s1 → s2 = s1 does not create a new object. It gives the same object a second name, so changing s2 also changes s1. Fix: s2 = Student('Liam').
Q6
Student.name = 'Zoe' → Using the class name changes the attribute on the class, not on one object. To change only one student use the object name, like s1.name = 'Zoe'.
Section C: Definitions
Q1
s2 = Student('Liam') — Creates a second object from the same class. Each object keeps its own attribute values, so changing s2 does not change s1, but both use the same methods.
Section D: Fill in the Blanks
Q1
s1 = Student('Ava') s2 = Student('Liam')
Q2
s1 = Student('Ava') s2 = Student('Liam') print(s2.name) # Liam
Q3
s1 = Student('Ava') s2 = Student('Liam') s2.points = 5 # s1 is unchanged
Q4
s1.greet() # Hi, I am Ava s2.greet() # Hi, I am Liam
Q5
class Student: def __init__(self, name): self.name = name
Section E: Spot the Error
Q1
C — == compares values, so no attribute is stored. It should be self.name = name so every Student object keeps its own name.
Q2
C — The string is missing its closing quote. It should be print('Woof!'). Both Dog objects share this same bark method.
Q3
D — The class name is Car with a capital C, and Python is case sensitive. car is not defined, so it should be car1 = Car('red').
Q4
A — The keyword must be lowercase class. Without it you cannot create p1 and p2 from one Pet blueprint.
Q5
A — A class header must end with a colon. It should be class Player: so that a and b can both be created from it.