Robust & Secure Programming (AQA GCSE Computer Science)

Exam Questions

14 mins6 questions
1a2 marks

A programmer has started to write a program using Python. Their program is shown in Figure 6.

The program should generate and output 10 numbers, each of which is randomly selected from the numbers in a data structure called numbers.

The program uses the random module.

For example, random.randrange(0, 8) would generate a random integer between 0 and 7 inclusive.

One possible output from the finished program would be 11, 14, 14, 42, 2, 56, 56, 14, 4, 2

• Line numbers are included but are not part of the program.

Figure 6

1 import random
2 numbers = [ 11, 14, 56, 4, 12, 6, 42, 2 ]
3 count = 0
4 while count < 10:
5    count = count + 1
6    number = random.randrange(0, 8
7    print(numbers[count])

The program shown in Figure 6 contains a syntax error.

Identify two choices to indicate the statements that are true about syntax errors.

  • A syntax error can be found by testing boundary values in a program.

  • A syntax error is a mistake in the grammar of the code.

  • A syntax error is generally harder to spot than a logic error.

  • A syntax error will stop a program from running.

  • An example of a syntax error is trying to access the fifth character in a string which only contains four characters.

1b2 marks

The program shown in Figure 6 also contains a logic error.

Identify the line number that contains the logic error, and correct this line of the program.

Your corrected line must be written in Python.

Did this page help you?

12 marks

Figure 7 shows part of a program written in Python.

Figure 7

validChoice = False
while validChoice == False:
   choice = int(input('Enter your choice [1 - 10]'))
   if choice >= 1 and choice <= 10:
     validChoice = True
   else:
     print('Invalid choice')
print('Valid choice')

Complete the following test plan for the code shown in Figure 7.

Test type

Test data

Expected result

Normal data

5

Valid choice message displayed

Invalid data

Boundary data

Did this page help you?

21 mark

State the line number from the program in Figure 8 which contains the error that stops the program from sometimes working correctly

Figure 8

1 num1 = int(input())
2 num2 = int(input())
3 hcf = 1
4 count = 1
5 while count < num1:
6    if (num1 % count == 0 and num2 % count == 0):
7      hcf = count
8    count = count + 1
9 print(hcf)

Did this page help you?

31 mark

Describe one way that the program in Figure 3 could be made more robust.

Figure 3

def calculate(width, length, height):
  if height == -1:
     return width * length
  else:
     return width  length  height

numOne = int(input("Enter width: "))
numTwo = int(input("Enter length: "))
numThree = int(input("Enter height, –1 to ignore: "))

answer = calculate(numOne, numTwo, numThree)

if numThree == -1:
  print(f"Area {answer}")
else:
  print(f"Volume {answer}")

Did this page help you?

41 mark

Figure 9

1 names ← ['Natalie', 'Alex', 'Roshana']
2 scores ← [78, 81, 72, 27, 51, 54, 52, 55, 59]
3 count ← 0
4 FOR i ← 0 TO 2
5     person ← names[i]
6     OUTPUT 'Student: ', person
7     FOR j ← 0 TO 1
8         OUTPUT j + 1
9         result ← scores[i * 3 + j]
10        OUTPUT result
11        count ← count + 1
12    ENDFOR
13 ENDFOR

How could the error in the algorithm in Figure 9 be corrected?

Identify one choice

  • Change line number 3 to: count ← -1

  • Change line number 4 to: FOR i ← 1 TO 4

  • Change line number 7 to: FOR j ← 0 TO 2

  • Change line number 9 to: result ← scores[j * 3 + i]

Did this page help you?

5a4 marks

Figure 10 shows part of an algorithm that has been written in pseudocode.

There is an error in the algorithm.

The algorithm should:

  • get the start year and end year from the user

  • check that the start year is before the end year

  • check that the start year is before 2000

  • calculate the difference between the two years after a valid start year has been entered.

  • Line numbers are included but are not part of the algorithm.

Figure 10

1 validChoice ← False
2 REPEAT
3    difference ← –1
4    OUTPUT 'Enter a start year '
5    startYear ← USERINPUT
6    OUTPUT 'Enter an end year '
7    endYear ← USERINPUT
8    IF startYear ≥ endYear THEN
9      OUTPUT 'Start year must be before end year'
10    ELSE
11     IF startYear < 2000 THEN
12        OUTPUT 'Start year must be before 2000'
13     ELSE
14        validChoice ← True
15     ENDIF
16    ENDIF
17 UNTIL validChoice = True
18 difference ← endYear - startYear
19 OUTPUT difference

Table 1 shows three tests used to check the algorithm in Figure 10.

Complete the table to show what the values of the validChoice and difference variables would be for the given test data.

Table 1

Test type

Test data

validChoice

difference

Normal

startYear

1995

endYear

2010

Erroneous

startYear

2015

endYear

2000

Boundary

startYear

2000

endYear

2023

5b1 mark

The algorithm in Figure 10 contains a logic error on line 11.

Describe how the error on line 11 can be corrected.

Did this page help you?