Python · 4.1
Object Oriented Programming: Classes and Objects
Name
Class
Date
Mark / 29
Section A
Circle the letter of the correct answer. One mark each.
- 1What is the difference between a class and an object?1
- AA class is a blueprint, an object is a specific thing built from it
- BAn object is a blueprint for a class
- CA class can only be used once
- DThey are the same
- 2What does
__init__do?1- ADefines a new class
- BRuns automatically when a new object is created and sets up its data
- CDeletes an object
- DPrints the object
- 3What does
selfrefer to?1- AThe last object created
- BThe program
- CThe particular object the method is being called on
- DThe class itself
- 4What is an attribute?1
- AA piece of data belonging to an object
- BA function inside a class
- CThe name of the class
- DA type of loop
- 5What does
__str__control?1- AWhether two objects are equal
- BThe object's attributes
- CWhat is displayed when the object is printed
- DHow the object is created
- 6What is encapsulation?1
- AHiding a program's source code
- BBundling data with the methods that act on it and controlling outside access
- CCreating many objects from one class
- DOne class inheriting from another
- 7Why put validation inside a class rather than in the code that uses it?1
- AEvery use of the class then gets the validation automatically
- BPython requires it
- CIt runs faster
- DIt uses less memory
- 8What does a leading underscore on an attribute name signal?1
- AThat it cannot be changed
- BThat it is a method
- CThat it is a constant
- DThat it is internal and should not be accessed from outside
- 9What does the
@propertydecorator allow?1- AA class to inherit
- BA method to run automatically
- CAn object to be deleted
- DA method to be accessed like an attribute
- 10Two objects created from the same class:1
- AMust have identical attributes values
- BShare the same data
- CShare the same methods but have their own separate data
- DCannot exist at the same time
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1ExplainExplain the difference between a class and an object, using an example.3
- 2WriteWrite a class called Rectangle with a constructor taking width and height, and a method that returns the area.5
- 3ExplainExplain what is meant by encapsulation and why it is useful.4
- 4ExplainExplain the purpose of the self parameter in a Python class.3
- 5DescribeA class stores a student's mark. Describe how you would ensure the mark can never be set to a value outside 0 to 100.4
Mark scheme
Section A
Section B
1 Explain the difference between a class and an object, using an example. 3
One mark for each of the following, to a maximum of 3.
- A class is a template or blueprint defining attributes and methods
- An object is a specific instance created from that class
- Gives a valid example such as a Dog class with individual dogs as objects
Model answer. A class is a blueprint that defines what attributes and methods every object of that type will have, but it does not itself hold any particular data. An object is a specific instance created from that class, with its own values for each attribute. For example, a Dog class might define that every dog has a name and a breed and can bark. Creating rex = Dog("Rex", "Labrador") and bella = Dog("Bella", "Collie") produces two separate objects from that one class, sharing the same methods but each holding its own name and breed.
2 Write a class called Rectangle with a constructor taking width and height, and a method that returns the area. 5
One mark for each of the following, to a maximum of 5.
- Defines the class with a suitable name
- Defines __init__ with self, width and height
- Assigns the parameters to attributes using self
- Defines a method taking self
- Returns width multiplied by height
Model answer. class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height r = Rectangle(5, 3) print(r.area()) # 15 The constructor runs automatically when a Rectangle is created and stores the width and height as attributes of that particular object. The area method takes self as its first parameter so that it can access those attributes, and it returns the result rather than printing it, so the value can be used in further calculations.
3 Explain what is meant by encapsulation and why it is useful. 4
One mark for each of the following, to a maximum of 4.
- Data and the methods that operate on it are bundled together in a class
- Access to the data from outside is controlled
- The object can therefore validate any change to its data
- This prevents other parts of the program putting the object into an invalid state
Model answer. Encapsulation means bundling an object's data together with the methods that operate on that data inside a single class, and controlling how the data can be accessed from outside. Rather than allowing any part of the program to change an attribute directly, changes are made through methods or properties belonging to the class. This is useful because it means the object can validate every change before accepting it, so a bank account can refuse a withdrawal that would take the balance below zero, and a temperature can refuse a value below absolute zero. Because the rule lives inside the class, it applies automatically everywhere the class is used, and no other part of the program can accidentally or deliberately put the object into a state that should be impossible.
4 Explain the purpose of the self parameter in a Python class. 3
One mark for each of the following, to a maximum of 3.
- It refers to the particular object the method is being called on
- It allows the method to access that object's own attributes
- Different objects therefore produce different results from the same method
Model answer. The self parameter refers to the specific object on which a method has been called. When rex.bark() is executed, Python passes rex in as self, so inside the method self.name refers to rex's name rather than any other dog's. This is what allows every object created from the same class to share the same method code while behaving differently, because each one has its own set of attribute values. Without self, a method would have no way of knowing whose data it was supposed to be working with.
5 A class stores a student's mark. Describe how you would ensure the mark can never be set to a value outside 0 to 100. 4
One mark for each of the following, to a maximum of 4.
- Store the mark in a private attribute with a leading underscore
- Provide a setter method or property to change it
- Validate the value inside that method before assigning it
- Reject or raise an error for invalid values
Model answer. The mark would be stored in a private attribute such as self._mark, with the leading underscore signalling that it is internal and should not be accessed directly from outside the class. Access would then be provided through a property, with a getter that simply returns the value and a setter that checks the new value before accepting it. The setter would test whether the value is between 0 and 100 inclusive and assign it only if so, raising a ValueError otherwise. Because the check lives inside the class, every piece of code that ever sets a mark goes through it automatically, so it is impossible for any part of the program to leave a student object holding an invalid mark.