Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · A Level 3

Object-Oriented Design

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 19 marks. Total 29 marks.

Name

Class

Date

Mark  / 29

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What is the difference between a class attribute and an instance attribute?1
    1. AA class attribute can only be a number, an instance attribute can be anything
    2. BA class attribute is created each time an object is made
    3. CA class attribute is private, an instance attribute is public
    4. DA class attribute is shared by all objects, an instance attribute belongs to one
  2. 2What does super().__init__(name, base) do in a subclass constructor?1
    1. ACreates a second copy of the subclass object and links the two together
    2. BOverrides every inherited method with an empty version of it
    3. CRuns the superclass constructor so the inherited attributes are set
    4. DDeletes the superclass so that the subclass can stand on its own
  3. 3What happens when you try to create an object from an abstract class?1
    1. AA TypeError is raised
    2. BThe object is created with empty attributes
    3. CNothing, because abstract classes are ignored
    4. DThe first subclass is created instead
  4. 4Which relationship does class Car having an Engine object that it creates and owns describe?1
    1. AInheritance
    2. BPolymorphism
    3. CAssociation
    4. DComposition
  5. 5What is polymorphism?1
    1. AA subclass that inherits from more than one superclass at the same time
    2. BHiding the attributes of an object so they cannot be read from outside
    3. CA class that has more than one constructor with different parameters
    4. DThe same method call behaving differently depending on the class of the object
  6. 6In a UML class diagram, what does a - in front of an attribute mean?1
    1. AProtected
    2. BPrivate
    3. CStatic
    4. DPublic
  7. 7Which line in a UML diagram means inheritance?1
    1. AA solid line with a hollow triangle at the parent class
    2. BA plain line with no end markers
    3. CA line with a filled diamond at the whole
    4. DA dashed line with an open arrowhead
  8. 8What does _balance with one leading underscore indicate in Python?1
    1. AIt is a constant that cannot be changed
    2. BIt is protected by convention and should not be used from outside the class
    3. CIt is a class attribute
    4. DIt will be deleted when the object is destroyed
  9. 9Why is it often better to favour composition over inheritance?1
    1. AInheritance cannot be used with abstract classes
    2. BComposition always runs faster
    3. CBehaviour can be changed or added without altering existing classes
    4. DComposition removes the need for constructors
  10. 10What does a @staticmethod have access to?1
    1. ANeither the object nor the class, unless they are passed in as arguments
    2. BOnly the private attributes of the object, and none of the public ones
    3. CThe attributes of the class, through the cls parameter that is passed in
    4. DThe attributes of the object, through the self parameter that is passed in

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1ExplainExplain what is meant by encapsulation, and give one benefit of using it.3
  2. 2ExplainExplain the difference between a class and an object, using an example.3
  3. 3WriteA Vehicle class has a private attribute registration, a protected attribute speed that starts at 0, a method accelerate(amount) that increases the speed, and a method get_speed(). A subclass Lorry has an extra attribute load, and overrides accelerate so that the speed can never go above 60. Write Python class definitions for Vehicle and Lorry.6
  4. 4ExplainExplain what is meant by polymorphism, using the shape classes in your answer as an example.3
  5. 5DiscussA game has a Character class. Some characters can fly, some can swim and some can do both. Discuss whether inheritance or composition is the better way to model how a character moves.4

Mark scheme

Section A

1D2C3A4D5D6B7A8B9C10A

Section B

1 Explain what is meant by encapsulation, and give one benefit of using it. 3

One mark for each of the following, to a maximum of 3.

  • Data and the methods that work on it are combined in one class
  • Access to the data from outside the class is restricted, for example by making attributes private
  • Benefit: the data can only be changed through methods that check it is valid, so it stays consistent

Model answer. Encapsulation means that the data of an object and the methods that operate on it are combined in a single class, and access to the data from outside is restricted, usually by making the attributes private and providing methods to read or change them. The benefit is that the data can only be changed through methods that can check it is valid, so an object cannot be put into an inconsistent state by other parts of the program.

2 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 that defines attributes and methods
  • An object is an instance of a class, created from it
  • An example, such as Dog as a class and rex as an object, where each object has its own attribute values

Model answer. A class is a template that defines the attributes and methods that its objects will have. An object is an instance of a class, created from that template, and it has its own values for the attributes. For example, a Dog class defines a name and a bark method, and rex and bella are two separate Dog objects, each with their own name.

3 A Vehicle class has a private attribute registration, a protected attribute speed that starts at 0, a method accelerate(amount) that increases the speed, and a method get_speed(). A subclass Lorry has an extra attribute load, and overrides accelerate so that the speed can never go above 60. Write Python class definitions for Vehicle and Lorry. 6

One mark for each of the following, to a maximum of 6.

  • Vehicle class with a constructor that sets registration as private and speed as 0
  • accelerate adds amount to speed
  • get_speed returns the speed
  • Lorry inherits from Vehicle
  • Lorry constructor calls super().__init__ and sets load
  • Lorry accelerate overrides, restricting the speed to a maximum of 60

Model answer.

Python
class Vehicle:
    def __init__(self, registration):
        self.__registration = registration
        self._speed = 0

    def accelerate(self, amount):
        self._speed += amount

    def get_speed(self):
        return self._speed

class Lorry(Vehicle):
    def __init__(self, registration, load):
        super().__init__(registration)
        self.load = load

    def accelerate(self, amount):
        self._speed = min(self._speed + amount, 60)

truck = Lorry("AB12 CDE", 8000)
truck.accelerate(45)
truck.accelerate(45)
print(truck.get_speed())

Vehicle keeps registration private and speed protected, so the subclass can use it. Lorry inherits from Vehicle, calls the superclass constructor to set up the inherited attributes, and overrides accelerate so that the speed is capped at 60. The calls shown print 60.

4 Explain what is meant by polymorphism, using the shape classes in your answer as an example. 3

One mark for each of the following, to a maximum of 3.

  • The same method name can be called on objects of different classes
  • Each class provides its own version of the method, which overrides the one in the superclass
  • The version that runs depends on the class of the object, so area() gives a different calculation for a circle and a rectangle

Model answer. Polymorphism means that the same method can be called on objects of different classes and each responds in its own way. Each subclass provides its own version of the method, overriding the one in the superclass. For shapes, calling area() on a Circle object calculates pi times the radius squared and calling it on a Rectangle calculates width times height, and the program does not need to know which class it has because the object's class decides which version runs.

5 A game has a Character class. Some characters can fly, some can swim and some can do both. Discuss whether inheritance or composition is the better way to model how a character moves. 4

One mark for each of the following, to a maximum of 4.

  • Inheritance gives a fixed is-a hierarchy, and a character that can do both cannot easily inherit from two separate parents
  • Composition lets each ability be a separate class held as a part
  • New abilities can be added or swapped without changing existing classes
  • Reasoned conclusion: composition is better because the abilities vary and combine

Model answer. Composition is the better choice. With inheritance, flying and swimming would each be a subclass, and a character that can do both would need to inherit from two parents or have a third class made for the combination, and every new ability would multiply the hierarchy. With composition each ability, such as Flying or Swimming, is its own class and a character holds the ones it has as attributes, so any combination is possible. A new ability can be added, or one swapped at run time, without changing any existing class, so composition suits behaviour that varies and combines.