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

Data types, operations and programming constructs

10 questions52 marksabout 60 min

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

1Standard4 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]

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

3Core4 marks

A shop's till program calculates the price of items including VAT. The VAT rate (20%) is written as the value 0.2 in eleven different places in the program.

(a) State two differences between a variable and a constant. [2]

(b) The programmer decides to replace each 0.2 with a named constant called VAT_RATE.
Explain two advantages of doing this. [2]

Stuck on Data types, operations and programming constructs?

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.

4Core6 marks

A program contains the following assignments.

x ← 23
y ← 4
word ← "12"

State the value produced by each of the following expressions.

(a) x DIV y [1]

(b) x MOD y [1]

(c) x / y [1]

(d) y ^ 3 [1]

(e) TRUNCATE(-7.6), where TRUNCATE removes the fractional part of a real number [1]

(f) word + "3" [1]

5Standard4 marks

The variables below have been assigned values.

a ← 7
b ← 12
c ← True

For each Boolean expression, state whether it evaluates to True or False.

(a) (a > 5) XOR (b > 10) [1]

(b) (NOT (a = b)) AND c [1]

(c) (a ≥ 8) OR ((b < 3) AND (NOT c)) [1]

(d) NOT ((a < b) AND (b MOD a = 5)) [1]

6Hard5 marks

The function below is written in pseudo-code. Strings are indexed from 0, and CHAR_TO_CODE returns the ASCII code of a character, e.g. CHAR_TO_CODE('A') returns 65 and CHAR_TO_CODE('Z') returns 90. CODE_TO_CHAR does the reverse, returning the character for an ASCII code.

FUNCTION Shift(word, k)
  result ← ''
  FOR i ← 0 TO LEN(word) - 1
    code ← CHAR_TO_CODE(word[i]) + k
    IF code > 90 THEN
      code ← code - 26
    ENDIF
    result ← result + CODE_TO_CHAR(code)
  ENDFOR
  RETURN result
ENDFUNCTION

(a) Complete a trace table with columns i, code and result for the call Shift('ZEBRA', 3). [4]

(b) Explain the purpose of the IF statement in the function. [1]

7Standard5 marks

The pseudo-code below unlocks a device if the correct PIN is entered.

1  CONSTANT MAX_TRIES ← 3
2  attempts ← 0
3  REPEAT
4     pin ← USERINPUT
5     attempts ← attempts + 1
6  UNTIL pin = '4071' OR attempts = MAX_TRIES
7  IF pin = '4071' THEN
8     OUTPUT 'Unlocked'
9  ELSE
10    OUTPUT 'Locked'
11 ENDIF

(a) Give the line number of:

(i) a constant declaration [1]

(ii) a statement that uses selection. [1]

(b) State whether the loop on lines 3–6 is an example of definite or indefinite iteration. Justify your answer. [2]

(c) Explain why a REPEAT … UNTIL loop is more suitable here than a WHILE … ENDWHILE loop. [1]

8Standard6 marks

A library system stores details of each loan using a user-defined record type, Loan. Some of its fields are shown below.

FieldExample value
MemberIDM00417
DueDate14/11/2026
FineOwed1.35
IsOverdueTrue

(a) State the most appropriate data type for each field. [4]

(b) The programmer stores all loans in a single array of Loan records rather than in four separate arrays (one for each field).
Explain one advantage of this. [2]

9Hard6 marks

The Python subroutine below uses exception handling.

def safe_divide(first, second):
    try:
        n = int(first)
        d = int(second)
        return "Result " + str(n // d)
    except ValueError:
        return "Not a whole number"
    except ZeroDivisionError:
        return "Cannot divide by zero"

(a) State what is returned by each of the following calls.

(i) safe_divide("45", "6") [1]

(ii) safe_divide("45", "0") [1]

(iii) safe_divide("4.5", "2") [1]

(iv) safe_divide("seven", "0") [1]

(b) Explain what is meant by exception handling and why it is used. [2]

10Stretch6 marks

A website gives each new user a random 8-character code made only of the upper-case letters A to Z. To make codes easier to read, no two adjacent characters may be the same (e.g. KXRRTPAB is not allowed).

The ASCII code of A is 65 and of Z is 90.

(a) Write a program that generates and outputs one valid code. You may use a function RANDOM_INT(low, high) that returns a random integer between low and high inclusive. [5]

(b) The random numbers produced by a computer are described as pseudo-random. Explain why. [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