Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Course:
Topics:
OCR GCSE Computer Science (Paper 2) Smart practice A mixed round aimed at your weak spots
Unit 1
Algorithms
Unit 2
Programming fundamentals
Unit 3
Producing robust programs
Unit 4
Boolean logic
Unit 5
Programming languages and IDEs
Unit 6
Practical programming (Section B)
Avg
score:
–
Topics
done:
0/31

Defensive design: anticipating misuse and authentication

Practise 25 exam-style questions on Defensive design: anticipating misuse and authentication for OCR GCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with the Study Helper on hand if you get stuck.

  • 25 questions
  • 1–6 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

Explain what is meant by anticipating misuse when designing a program.

Model answer

Anticipating misuse means the programmer thinks in advance about the ways a user could use the program wrongly, such as typing letters instead of a number, leaving an input blank or trying to break in. The programmer then writes code to deal with each of these, so the program keeps working correctly instead of crashing.

Explanation

Users do not always do what the programmer expects: some make mistakes and some deliberately try to break or misuse a program. A defensively designed program caters for all likely input values, for example by validating inputs and limiting login attempts. Both parts are needed: predicting the misuse, and then handling it.

Mark scheme
  1. 1 mark

    thinking ahead about / predicting the ways a user might use the program wrongly or enter unexpected data (by accident or on purpose)

    Accept a correct example, e.g. 'predicting that a user might type letters when a number is expected'.

  2. 1 mark

    and planning / writing code to deal with them, so the program does not crash or give incorrect results

    Accept 'so that the program caters for all likely input values'.

Question 2

3 marks

A restaurant booking program asks:

How many people are in your group (1 to 10)?

Identify three different kinds of unexpected input that a user might enter. Give an example of each.

Model answer

A number that is too big, such as 25.
A negative number, such as -3.
Text instead of a number, such as four.

Explanation

Anticipating misuse starts with listing the kinds of input a user might give: values out of range at either end, the wrong type (text or a decimal when a whole number is needed) and no input at all. Each kind then needs handling, for example with a range check, a type check and a presence check. Listing three numbers that are all too big only shows one kind of misuse.

Mark scheme
  1. 1 mark

    a number that is too large, e.g. 25

    1 mark for each different kind of input with a suitable example, max 3. Two examples of the same kind (e.g. 11 and 50) score only 1 mark.

  2. 1 mark

    zero or a negative number, e.g. 0 / -3

  3. 1 mark

    a decimal number, e.g. 2.5

  4. 1 mark

    text instead of a number, e.g. four

  5. 1 mark

    nothing at all / pressing Enter without typing anything

  6. 1 mark

    symbols or spaces mixed with digits, e.g. 4! / 1 0

Question 3

3 marks

Write a function called checkLogin that takes a username and a password as parameters. It should return true if the username is otter19 and the password is River#Stone4, and false otherwise.

Write the function using OCR Exam Reference Language or a high-level programming language.

Model answer

function checkLogin(user, pw)
    if user == "otter19" AND pw == "River#Stone4" then
        return true
    else
        return false
    endif
endfunction

Explanation

Putting the check in a function means the rest of the program can simply write if checkLogin(name, pword) then. The function must return the result rather than print it. Both comparisons are needed, joined with AND. In Python the body could be one line: return user == "otter19" and pw == "River#Stone4".

Mark scheme
  1. 1 mark

    function defined with the name checkLogin and two parameters

    Answers must be in OCR Exam Reference Language or a high-level programming language (e.g. Python); pseudocode, natural English or bullet points score 0. Ignore minor syntax errors.

  2. 1 mark

    checks that the username is otter19 and the password is River#Stone4 (both must match: AND / nested if)

    Do not award if OR is used.

  3. 1 mark

    returns true when both match and false otherwise

    Accept return (user == "otter19" AND pw == "River#Stone4"). Do not award if the result is only output.

Question 4

4 marks

A cinema booking program contains this code.

seatsLeft = 40
wanted = int(input("How many seats do you want?"))
seatsLeft = seatsLeft - wanted
print("Seats left: " + str(seatsLeft))

Describe two ways a user could misuse this code and, for each one, how the code should be changed to anticipate it.

Model answer

A user could ask for 50 seats when only 40 are left, which would make seatsLeft -10. The program should check that wanted is no more than seatsLeft before subtracting, and ask again if it is.

A user could enter -5, which would increase seatsLeft to 45. The program should check that wanted is at least 1 and ask again if it is not.

Explanation

The code trusts whatever the user types. A defensive programmer considers values that are too big, too small or the wrong type, and checks them before they are used in a calculation. Note that the upper limit here is not fixed: it depends on how many seats are left, so the check compares wanted with seatsLeft.

Mark scheme
  1. 1 mark

    asking for more seats than are left (e.g. 50) makes seatsLeft negative

    Up to 2 marks for each misuse: 1 for the misuse and its effect, 1 for a matching change. max 4.

  2. 1 mark

    check that wanted <= seatsLeft before subtracting, and ask again / output a message if not

  3. 1 mark

    entering 0 or a negative number (e.g. -5) does not reduce / increases seatsLeft

  4. 1 mark

    check that wanted is at least 1 (>= 1 / > 0), and ask again if not

  5. 1 mark

    entering text or a decimal (e.g. two / 2.5) makes int() fail, so the program crashes

  6. 1 mark

    check the input contains only digits (a type check) before converting it, and ask again if not

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

Study Helper Hints first, answers after you try

Stuck on this one? Ask for a hint and I’ll point you in the right direction, without giving the answer away. Once you’ve checked your answer, I’ll show you how the marks are awarded.