Hard4 marksStructured
Fundamentals of algorithmsGeneraltracedebuggingalgorithms

AQA GCSE · Question 11.1 · Fundamentals of algorithms

Test type Test data validChoice difference Normal startYear 1995 endYear 2010 Erroneous startYear 2015 endYear 2000 Boundary startYear 2000 endYear 2023

Figure 10 shows part of an algorithm that has been written in pseudo-code. 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.

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
Complete the table to show what the values of the validChoice and difference variables would be for the given test data after the program finishes.

How to approach this question

Trace the algorithm for each set of test data, paying close attention to the logic error. The loop only terminates when `validChoice` becomes `True`. 1. **Normal Data:** `startYear`=1995, `endYear`=2010. * `startYear >= endYear` is false. * The code enters the `ELSE` block (line 10). * `startYear < 2000` (1995 < 2000) is true. * The code executes line 12, outputting an error message. **This is the logic error.** * `validChoice` is never set to `True`. The loop is infinite. * Final value of `validChoice` is `False`. `difference` is never calculated. 2. **Erroneous Data:** `startYear`=2015, `endYear`=2000. * `startYear >= endYear` (2015 >= 2000) is true. * The code executes line 9, outputting an error message. * `validChoice` is never set to `True`. The loop is infinite. * Final value of `validChoice` is `False`. `difference` is never calculated. 3. **Boundary Data:** `startYear`=2000, `endYear`=2023. * `startYear >= endYear` is false. * The code enters the `ELSE` block (line 10). * `startYear < 2000` (2000 < 2000) is false. * The code enters the `ELSE` block (line 13). * `validChoice` is set to `True`. * The loop terminates because `validChoice` is `True`. * Line 18 executes: `difference ← 2023 - 2000`, so `difference` becomes 23. * Final values: `validChoice`=`True`, `difference`=23.

Full Answer

The completed table is: - **Normal:** The loop does not terminate. `validChoice` remains `False`, `difference` is not calculated. - **Erroneous:** The loop does not terminate. `validChoice` remains `False`, `difference` is not calculated. - **Boundary:** `validChoice` = `True`, `difference` = `23`.
This question requires tracing a buggy algorithm. The key is to follow the logic exactly as written. The logic error is on lines 11-14. The code should validate that the `startYear` is before 2000. However, it outputs an error message if `startYear < 2000` (which is a valid year) and sets `validChoice` to `True` if `startYear` is 2000 or greater (which is an invalid year). - **Normal Test (1995, 2010):** The condition `startYear < 2000` is true. The algorithm incorrectly prints an error and never sets `validChoice` to `True`. The `REPEAT...UNTIL` loop never ends. - **Erroneous Test (2015, 2000):** The condition `startYear >= endYear` is true. The algorithm prints an error and never sets `validChoice` to `True`. The loop never ends. - **Boundary Test (2000, 2023):** The condition `startYear >= endYear` is false. The condition `startYear < 2000` is also false. The `ELSE` block on line 13 is executed, setting `validChoice` to `True`. The loop terminates. The program then calculates `difference = 2023 - 2000 = 23`.

Common mistakes

✗ "Fixing" the logic error in your head and tracing the corrected version instead of the version provided.\n✗ Assuming the loop will terminate for the Normal and Erroneous cases.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam