OOP – Classes & Objects

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

OOP – Classes & Objects

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a class?
A A blueprint for creating objects
B A function
2. What is an object?
A A class definition
B An instance created from a class
3. How do you create an object?
A my_obj = ClassName()
B def ClassName():
4. What does 'self' refer to?
A The current object instance
B The class itself
5. class Dog:\n pass\ndog = Dog()\nWhat is dog?
A The Dog class
B An instance of the Dog class

Match the Pairs

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

Left columnRight column
1class dog: pass AOutput: Mia
2class Dog pass BError: Missing colon after class name
3class Dog: pass d = Dog() CError: Class names should be CapWords
4class Car: pass a = Car() b = Car() DOutput: <class '__main__.Dog'>
5class Dog: pass d = Dog() print(type(d)) EOk: Two separate objects
6class Cat: pass c = Cat() c.name = 'Mia' print(c.name) FValid: Class defined and instance created

Definitions

Class

Fill in the Blanks

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

1.
_____ Dog: pass
2.
class Dog: pass my_dog = ___()
3.
p = Person() p.____ = 'Alice'
4.
class Car: ____
5.
print(____(my_dog))

Spot the Error

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

Question 1
A Class Dog:
B def __init__(self, name):
C self.name = name
D def bark(self):
E return 'Woof!'
Question 2
A class dog:
B def __init__(self, name):
C self.name = name
D def bark(self):
E return 'Woof!'
Question 3
A class Dog
B def __init__(self, name):
C self.name = name
D def bark(self):
E return 'Woof!'
Question 4
A class Dog():
B pass
C
D my_dog = Dog
Question 5
A def Dog:
B def __init__(self, name):
C self.name = name
D def bark(self):
E return 'Woof!'

Answer Key

OOP – Classes & Objects
Section A: Quick Questions
Q1
A — A blueprint for creating objects
Q2
B — An instance created from a class
Q3
A — my_obj = ClassName()
Q4
A — The current object instance
Q5
B — An instance of the Dog class
Section B: Match the Pairs
Q1
class dog: pass → Error: Class names should be CapWords
Q2
class Dog pass → Error: Missing colon after class name
Q3
class Dog: pass d = Dog() → Valid: Class defined and instance created
Q4
class Car: pass a = Car() b = Car() → Ok: Two separate objects
Q5
class Dog: pass d = Dog() print(type(d)) → Output: <class '__main__.Dog'>
Q6
class Cat: pass c = Cat() c.name = 'Mia' print(c.name) → Output: Mia
Section C: Definitions
Q1
Class — A blueprint that defines the structure of objects.~~Object
Section D: Fill in the Blanks
Q1
class Dog: pass
Q2
class Dog: pass my_dog = Dog()
Q3
p = Person() p.name = 'Alice'
Q4
class Car: pass
Q5
print(type(my_dog))
Section E: Spot the Error
Q1
A — class must be lowercase: Class is not valid Python
Q2
A — Class names should use CamelCase: Dog not dog
Q3
A — Missing colon after the class name
Q4
D — Missing parentheses: Dog() creates an object, Dog is the class itself
Q5
A — Use class to define a class, not def (def is for functions)