Defensive Design & Testing (OCR GCSE Computer Science)

Topic Questions

52 mins16 questions
12 marks

Jack is writing a program to add up some numbers. His first attempt at the program is shown.

a = input("Enter a number")

b = input("Enter a number")

c = input("Enter a number")

d = input("Enter a number")

e = input("Enter a number")

f = (a + b + c + d + e)

print(f)

Give two ways that the maintainability of this program could be improved.

1

2

Did this page help you?

22 marks

A program should only allow values from 0 to 300 inclusive as valid inputs. If the data entered breaks this validation rule, an error message is displayed.

Complete the following test plan for the program.

Test data

Test type

Expected result

25

Normal

Value accepted

Invalid

Invalid input message displayed

300

Boundary

Did this page help you?

3a2 marks

The algorithm for one section of a vending machine program is shown in pseudocode.

if money >= price then

venditem()

giveChange(money – price)

else

print("Error – not enough money inserted")

endif

When writing the program maintainability was considered.

Identify two ways that the program has been made more maintainable.

1

2

3b1 mark

Give one additional way that the maintainability of the program can be improved.

Did this page help you?

13 marks

Customers at a hotel can stay between 1 and 5 (inclusive) nights and can choose between a basic room or a premium room.

Complete the following test plan to check whether the number of nights is validated correctly.

Test data

(number of nights)

Type of test

Expected output

2

ALLOWED

Boundary

ALLOWED

Erroneous / Invalid

NOT ALLOWED

Did this page help you?

24 marks

The area of a circle is calculated using the formula π × r2 where π is equal to 3.142 and r is the radius.

A program is written to allow a user to enter the radius of a circle as a whole number between 1 and 30, then calculate and output the area of the circle.

01 radius = 0

02 area = 0.0

03 radius = input("Enter radius")

04 if radius < 1 OR radius > 30 then

05 print("Sorry, that radius is invalid")

06 else

07 area = 3.142 * (radius ^ 2)

08 print (area)

09 endif

Explain, using examples from the program, two ways to improve the maintainability of the program.

1

2

Did this page help you?

33 marks

The program should only allow values from 0 to 300 inclusive as valid inputs. If the data entered breaks this validation rule, an error message is displayed.

Complete the following program to output "Invalid input" if the data does not meet the validation rule.

You must use either:

  • OCR Exam Reference Language, or

  • a high-level programming language that you have studied.

mins = input("Enter minutes played: ")

if mins < 0 …………………… mins …………………… then

…………………………… ("Invalid input")

endif

Did this page help you?

44 marks

A teacher writes an algorithm to store the name of the game a student plays each night (for example "OCR Zoo Simulator").

variable.length returns the number of characters in variable. variable.upper returns the characters in variable in upper case.

valid = false

while(valid == false)

gameName = input("Enter the game name")

if (gameName.length > 0) AND (gameName.length < 20)

gamesPlayed = gameName.upper

valid = true

print("Valid game name")

else

print("Game name is not valid")

endif

endwhile

The algorithm needs testing to make sure the IF-ELSE statement works correctly.

Identify two different pieces of test data that can be used to test different outputs of the algorithm.

Give the output from the program for each piece of test data.

Test data

Output

Did this page help you?

5a1 mark

OCRBlocks is a game played on a 5 × 5 grid. Players take it in turns to place blocks on the board.

The board is stored as a two-dimensional (2D) array with the identifier gamegrid

Fig. 6.1 shows that players A and B have placed three blocks each so far.

Fig 6.1_Paper 2_Nov 2020_OCR GCSE Computer Scienc

The function checkblock() checks whether a square on the board has been filled. When checkblock(4,2) is called, the value "A" is returned.

function checkblock(r,c)

if gamegrid[r,c] == "A" or gamegrid[r,c] == "B" then

outcome = gamegrid[r,c]

else

outcome = "FREE"

endif

return outcome

endfunction

When checkblock(-1,6) is called, an error is produced

State why this function call will produce an error.

5b3 marks

Describe how validation could be added in to the checkblock() function to stop this error from occurring.

Did this page help you?

6a2 marks

A vending machine has the following options available.

Item code

Item name

Price

A1

Crisps, bacon flavour

£0.75

A2

Crisps, salted

£0.75

B1

Chocolate bar

£0.90

C1

Apple pieces

£0.50

C2

Raisins

£0.85

Users insert coins into the vending machine and then enter the two character item code of their selection. If the user has inserted enough money, the vending machine will release the chosen item and output any change required. If the user enters an invalid item code then a suitable error message is displayed.

The vending machine is tested before it is released.

Explain the purpose of testing the vending machine.

6b2 marks

Describe the difference between iterative testing and final testing.

6c1 mark

Complete the following test plan for the vending machine

Code entered

Money inserted

Expected result

B1

£1

Chocolate bar served, £0.10 change given

£0.85

Raisins served, no change given

C1

Error – not enough money inserted

C3

£0.75

Did this page help you?

7a2 marks

Louise writes a program to work out if a number entered by the user is odd or even. Her first attempt at this program is shown.

01 num = input(“enter a number”)

02 if num MOD 2 >= 0 then

03 print(“even”)

04 else

05 pritn(“odd”)

06 endif

The program contains a syntax error on line 05.

State what is meant by a syntax error.

7b1 mark

Give a corrected version of line 05 that fixes the syntax error.

Did this page help you?

84 marks

Elliott plays football for OCR FC. He wants to create a program to store the results of each football match they play and the names of the goal scorers. Elliott wants individual players from the team to be able to submit this information.

Describe two examples of defensive design that should be considered when developing this program.

1

2

Did this page help you?

12 marks

A hotel has nine rooms that are numbered from room 0 to room 8.

The number of people currently staying in each room is stored in an array with the identifier room.

The index of room represents the room number.

Array room

Index

0

1

2

3

4

5

6

7

8

Data

2

1

3

2

1

0

0

4

1

The following program counts how many people are currently staying in the hotel.

for count = 1 to 8

total = 0

total = total + room[count]

next count

print(total)

When tested, the program is found to contain two logic errors.

Describe how the program can be refined to remove these logic errors.

Did this page help you?

2a1 mark

Louise writes a program to work out if a number entered by the user is odd or even. Her first attempt at this program is shown.

01 num = input(“enter a number”)

02 if num MOD 2 >= 0 then

03 print(“even”)

04 else

05 pritn(“odd”)

06 endif

The program contains a logic error on line 02.

State what is meant by a logic error.

2b1 mark

Give a corrected version of line 02 that fixes the logic error.

Did this page help you?

32 marks

Describe the purpose of input validation in defensive programming.

Did this page help you?

45 marks

Write a Python function that validates if an input string is a valid email address (contains "@" and ".").

Did this page help you?

54 marks

Describe the differences between authentication and input validation.

Did this page help you?