Question bank › A-level › A-level Computer Science › Fundamentals of programming

Programming paradigms and object-oriented programming

10 questions51 marksabout 60 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core3 marks

(a) State what is meant by encapsulation in object-oriented programming. [1]

(b) Explain two differences between a local variable and a global variable. [2]

2Standard6 marks

The Python program below defines two classes.

class Vehicle:
    def __init__(self, reg, wheels):
        self.__reg = reg
        self._wheels = wheels

    def describe(self):
        return self.__reg + " has " + str(self._wheels) + " wheels"

class Lorry(Vehicle):
    def __init__(self, reg, wheels, maxLoad):
        super().__init__(reg, wheels)
        self.__maxLoad = maxLoad

    def describe(self):
        return super().describe() + ", max load " + str(self.__maxLoad) + " tonnes"

l = Lorry("KX70 ABC", 6, 26)
print(l.describe())

(a) Identify the name of the subclass. [1]

(b) Identify a method that has been overridden. [1]

(c) State the exact output when the program is run. [2]

(d) The attribute __reg is private. Explain why a programmer would make an attribute private and how other objects could still be allowed to read its value. [2]

3Stretch6 marks

The class diagram shows part of a drawing application. Shape is an abstract class; its method area is abstract (shown in italics).

(a) State the type of relationship between Drawing and Shape shown by the hollow diamond, and explain what it means for the lifetime of the Shape objects. [2]

(b) Explain why area has been declared as an abstract method in Shape rather than being implemented there. [2]

(c) The attribute shapes is a list of Shape objects. Write the method totalArea for the Drawing class so that it returns the total area of all the shapes in the drawing. Your method must make use of polymorphism. You may use pseudo-code or a programming language you have studied. [2]

Drawing- shapes+ add(s)+ totalArea() Shape + area() Circle- radius+ area() Rectangle- width- height+ area() 0..*
Stuck on Programming paradigms and object-oriented programming?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core4 marks

A school uses a procedural program to process exam results. The hierarchy chart for the program is shown in the diagram.

(a) State what a hierarchy chart shows about a program. [1]

(b) Identify the subroutine that calls ValidateMark. [1]

(c) The program was designed using a structured approach. Explain two advantages of the structured approach to program design. [2]

ProcessResults GetMarksCalculateGradesOutputReport ReadFileValidateMarkLookUpGrade
5Core4 marks

A team of developers is writing a large booking system using the object-oriented paradigm.

(a) Explain the difference between a class and an object. [2]

(b) Explain two reasons why the object-oriented paradigm is often chosen for a large program developed by a team. [2]

6Standard5 marks

The Python program below is used by a raffle to give each ticket a unique number.

class Ticket:
    _nextNumber = 100

    def __init__(self, holder):
        self.__holder = holder
        self.__number = Ticket._nextNumber
        Ticket._nextNumber = Ticket._nextNumber + 5

    @staticmethod
    def peekNext():
        return Ticket._nextNumber

    def label(self):
        return self.__holder + "-" + str(self.__number)

a = Ticket("Ana")
b = Ticket("Ben")
print(a.label())
print(Ticket.peekNext())
c = Ticket("Cal")
print(c.label(), b.label())

(a) State the exact output when the program is run. [3]

(b) peekNext is a static method and _nextNumber is shared by the whole class. Explain what is meant by a static method and why it is appropriate for peekNext to be static. [2]

7Standard5 marks

The class diagram shows part of a hotel management system. The filled diamond is next to Hotel.

(a) Name the type of relationship between Hotel and Room and explain what it means for the Room objects when a Hotel object is destroyed. [2]

(b) nightlyRate has the symbol # but number has the symbol -. Explain the difference in how these two attributes can be accessed, and why nightlyRate may have been given #. [2]

(c) Identify the method that is overridden in the diagram. [1]

Hotel - name : String- rooms : List + addRoom(num, rate)+ findRoom(num) Room - number : Integer# nightlyRate : Real + getNumber()+ getPrice(nights) Suite - butlerFee : Real + getPrice(nights) 1..*
8Hard6 marks

A games company is writing an object-oriented game. Every enemy has one way of moving (walk, fly or swim) and one way of attacking (bite, shoot, freeze or explode). The first design uses a base class Enemy with a separate subclass for every possible combination, for example FlyingShootingEnemy.

(a) Calculate the number of subclasses of Enemy needed in the first design so that every combination is possible. [1]

(b) Explain two disadvantages of the first design. [2]

A second design follows the principles “encapsulate what varies” and “favour composition over inheritance”. A single Enemy class has an attribute mover that holds a MoveBehaviour object and an attribute attacker that holds an AttackBehaviour object. MoveBehaviour and AttackBehaviour are abstract classes with one concrete subclass for each way of moving or attacking.

(c) Explain how the second design applies these two principles and why it is an improvement on the first design. [3]

9Hard6 marks

The Python program below models bank accounts. In Python every method can be overridden (all methods behave as virtual methods).

class Account:
    def __init__(self, balance):
        self._balance = balance

    def fee(self):
        return 2

    def monthEnd(self):
        self._balance = self._balance - self.fee()
        return self._balance

class Saver(Account):
    def fee(self):
        if self._balance >= 500:
            return 0
        return 1

class Junior(Saver):
    def monthEnd(self):
        self._balance = self._balance + 3
        return super().monthEnd()

accounts = [Account(50), Saver(500), Junior(495), Junior(497)]
for acc in accounts:
    print(acc.monthEnd())

(a) State the output of the program. [4]

(b) The method monthEnd is defined only in Account and Junior, but the fee charged depends on the class of the object. Explain how this happens. [2]

10Stretch6 marks

A greenhouse control system contains the classes below. Sensor is an abstract class. Concrete subclasses such as Thermometer and HumiditySensor each override read, which returns the sensor's current value as a real number.

from abc import ABC, abstractmethod

class Sensor(ABC):
    def __init__(self, name):
        self.__name = name

    def getName(self):
        return self.__name

    @abstractmethod
    def read(self):
        pass

class Monitor:
    def __init__(self, sensors):
        self.__sensors = sensors    # a list of Sensor objects

    def countAbove(self, limit):
        # to be written

(a) Write the method countAbove so that it returns the number of sensors in __sensors whose current reading is greater than limit. Your method must work for any type of sensor, including types added in future. You may use pseudo-code or a programming language you have studied. [3]

(b) The design follows the principle “program to interfaces, not implementation”. Explain what this means in this system and how it helps when a new type of sensor, LightSensor, is added. [2]

(c) A Monitor object is given a list of sensor objects that were created elsewhere in the program. State, with a reason, whether the relationship between Monitor and Sensor is aggregation or composition. [1]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session