Question bank › A-level Computer Science › Paper 1 – Programming, data structures & algorithms

Programming fundamentals & OOP (classes, inheritance, polymorphism)

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]

2Standard4 marks

The algorithm below is written in pseudo-code.

n ← 2026
total ← 0
WHILE n > 0
   total ← total + (n MOD 10)
   n ← n DIV 10
ENDWHILE
OUTPUT total

(a) Complete a trace table, with columns n and total, for the algorithm. Show the initial values in the first row. [3]

(b) State the purpose of the algorithm. [1]

3Standard5 marks

The recursive function F is shown below.

FUNCTION F(a, b)
   IF b = 0 THEN
      RETURN a
   ELSE
      RETURN F(b, a MOD b)
   ENDIF
ENDFUNCTION

(a) State the value returned by the call F(84, 36). [1]

(b) State how many times F is called in total, including the original call, when F(84, 36) is executed. [1]

(c) State what the function F calculates. [1]

(d) Identify the base case in F and explain why a recursive subroutine must have a base case. [2]

4Standard6 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]

5Standard6 marks

Write a program that:

  • repeatedly asks the user to enter a password until the password entered is valid
  • a valid password has at least 8 characters and contains at least one digit (0–9)
  • displays a suitable message each time an invalid password is entered
  • once a valid password has been entered, displays the number of attempts the user took.

You may write your answer in pseudo-code or a programming language you have studied. [6]

6Hard4 marks

Consider the Python program below.

def change(x, items):
    x = x + 1
    items.append(x)

a = 5
b = [1]
change(a, b)
print(a, b)

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

(b) Explain why the value of a and the contents of b are affected differently by the call to change. [2]

7Stretch6 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..*

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro