Question bank › GCSE › GCSE Computer Science › Producing robust programs

Defensive design: validation, authentication, maintainability

10 questions43 marksabout 50 min

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

1Grade 1–32 marks

Give two ways a programmer can make their code easier to maintain. [2]

2Grade 4–54 marks

A school's online form collects pupil details. Name the most suitable validation check for each input. [4]

InputRule
AgeMust be between 11 and 18
PostcodeMust follow the pattern of a UK postcode
SurnameMust not be left blank
PasswordMust be at least 8 characters
3Grade 6–74 marks

Write code that asks the user to enter their age. The program must keep asking until an age between 11 and 18 inclusive is entered. Use OCR Exam Reference Language or a high-level programming language. [4]

Stuck on Defensive design: validation, authentication, maintainability?

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.

4Grade 8–96 marks

A system stores the username "admin" and the password "Pa55word".

Write an algorithm that:

• asks the user to enter a username and password

• allows a maximum of 3 attempts

• outputs "Access granted" if both are correct

• outputs "Locked out" after 3 incorrect attempts.

Use OCR Exam Reference Language or a high-level programming language. [6]

5Grade 1–33 marks

A gym uses a mobile app. Members must prove who they are before the app shows their membership details.

(a) State what is meant by authentication. [1]

(b) Give one way the app could authenticate a member. [1]

(c) When a member books a class, the app checks that the number of guests entered is a whole number. State the name given to this type of check. [1]

6Grade 4–54 marks

A cinema's ticket program contains the following code.

tickets = int(input("How many tickets?"))
cost = tickets * 12.50
print("Total: " + str(cost))

(a) A customer types two when asked for the number of tickets. State what happens when the program runs, and explain why. [2]

(b) The programmer is told to anticipate misuse of the program. Give two different values a customer could enter that the program would accept but which are not sensible, and state what the program should do instead of calculating the cost. [2]

7Grade 4–54 marks

An online bank app allows customers to transfer money.

(a) Describe two different ways the app could authenticate a customer when they log in. [2]

(b) Before a transfer is made, the app checks that the amount entered is greater than 0. A student says this is also an example of authentication.

Explain why the student is wrong. [2]

8Grade 6–74 marks

A programmer has written the following program to work out whether a parcel can be sent by standard post.

a = int(input())
b = int(input())
c = int(input())
d = a * b * c
if d < 9000 then
print("yes")
else
print("no")
endif

The program works correctly, but another programmer says it is difficult to maintain.

Identify two features of the program that make it difficult to maintain. For each feature, describe how the program could be improved. [4]

9Grade 6–76 marks

A warehouse uses product codes. A valid code is 6 characters long: two upper-case letters followed by four digits, e.g. AB1234.

The function below is meant to validate a product code.

function isValidCode(code)
    if code.length != 6 then
        return "Wrong length"
    endif
    letters = code.substring(0, 2)
    digits = code.substring(2, 4)
    if letters.upper != letters then
        return "Letters not upper case"
    endif
    for i = 0 to 3
        if digits.substring(i, 1) < "0" OR digits.substring(i, 1) > "9" then
            return "Not digits"
        endif
    next i
    return "Valid"
endfunction

(a) Complete the table to show the value returned by the function for each code. [4]

CallValue returned
isValidCode("ab1234") 
isValidCode("AB12345") 
isValidCode("AB12X4") 
isValidCode("A11234") 

(b) One of the codes in the table is accepted by the function even though it is not a valid product code. Explain why the function accepts it. [2]

10Grade 8–96 marks

A door-entry system asks the user to type a PIN as a string. A PIN is only valid if it is exactly 4 characters long and every character is a digit (0 to 9).

(a) Write a function validPIN(pin) that takes the string pin as a parameter and returns true if the PIN is valid and false otherwise. [5]

(b) Write a program that asks the user to enter a PIN and uses your function to keep asking until a valid PIN is entered. [1]

Use OCR Exam Reference Language or a high-level programming language.

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