Chhetri AcademyGCSE & A level Paper Builder

2.3.1Defensive design

OCR GCSE Computer Science (J277) · Algorithms and programming (Paper 2) › Producing robust programs

Practise Defensive design. 11 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.

Build a paper on this topic

▶ Watch videos on Defensive design (Craig 'n' Dave OCR on YouTube) · Practise all of Producing robust programs

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

State the name of this type of validation check.
Range check
State the name of the type of validation check that checks the pattern of letters and digits.
Format check

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy3 marks
(a) State what is meant by authentication.[1]
(b) Give one method of authentication.[1]
(c) Give one way that a programmer can anticipate misuse of a program.[1]
Show the answer and mark scheme
(a)
  • confirming / checking the identity of a user (before they are allowed to access the program or data)
(b)
  • username and password / PIN / fingerprint or other biometric / a code sent to a phone (two-factor authentication)
(c)
  • validate every input
  • limit the number of login attempts
  • check for unexpected inputs, e.g. text where a number is expected / blank inputs
  • prevent errors such as dividing by zero
Question 2Medium6 marks
Look at this program.
01 // Works out the price after a percentage discount
02 function discountedPrice(price, percent)
03    discount = price * percent / 100
04    return price - discount
05 endfunction
06 
07 // Main program
08 itemPrice = float(input("Enter the price"))
09 print(discountedPrice(itemPrice, 15))
Identify three features of the program that make it easier to maintain. For each feature, explain how it helps.[6]
Show the answer and mark scheme
  • comments (lines 01 and 07)
  • explain what the code does, so another programmer can understand it quickly
  • meaningful / descriptive identifiers, e.g. discountedPrice, itemPrice
  • make the purpose of each variable and sub program clear
  • indentation (lines 03 and 04)
  • shows which lines belong inside the function / makes the structure clear
  • use of a sub program (function)
  • the code can be reused and only needs to be changed in one place
Question 3Hard8 marks
A program stores a user's username and password in the variables storedUser and storedPass.
(a) Write an algorithm that:
  • asks the user to enter a username and a password
  • allows a maximum of three attempts
  • outputs Welcome if the username and password both match the stored values
  • outputs Account locked after three failed attempts.
You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[6]
(b) Explain why the program should not tell the user whether it was the username or the password that was wrong.[2]
Show the answer and mark scheme
(a) Answer:
attempts = 0
success = false
while attempts < 3 AND success == false
   user = input("Username")
   password = input("Password")
   attempts = attempts + 1
   if user == storedUser AND password == storedPass then
      success = true
   endif
endwhile
if success then
   print("Welcome")
else
   print("Account locked")
endif
  • inputs a username and a password inside the loop
  • compares both with the stored values using AND
  • counts the number of attempts
  • the loop ends when the details are correct OR three attempts have been used
  • outputs Welcome when the details match
  • outputs Account locked after three failed attempts
(b)
  • an attacker would learn that a username is valid
  • so they would only need to guess the password, making the account easier to break into

Related subtopics

Stuck? Get 1-to-1 help. Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.

Book a free trial