Average Marks Program with a Hidden Divisor Bug
An HSLC student writes a small program to read marks for six subjects and print the average and grade. The marks entered for one student are 72, 48, 39, 61, 55 and 45, each out of 100. The program stores the sum in a variable total and then computes average = total / 5. It prints Distinction if average >= 60, First Division if average >= 45, Pass if average >= 33, otherwise Fail. The student tests only with six marks of 60, sees 'Distinction', and submits the program. The teacher runs it with the six marks above and gets 'Distinction', which is wrong. The student says the logic is fine because the total is correct and the grade conditions are in the right order. The teacher wants the error identified, the correct output traced, and a test added at the exact boundary of 45.
1. Trace the program with the six marks. State the printed average and grade, identify the error, and give the corrected average.
Read the worked answer
Sum = 72 + 48 + 39 + 61 + 55 + 45 = 320. The program divides by 5, so it prints 320 / 5 = 64.0. Since 64 >= 60, it prints Distinction. The error is the hard-coded divisor 5. There are six subjects, so the average must be 320 / 6 = 53.333..., which rounds to 53.33. That value is at least 45 but below 60, so the correct grade is First Division. The total is indeed correct, but a correct numerator with the wrong denominator still gives a wrong average. If the language uses integer division, 320 // 6 would give 53 and lose the decimal; using a floating-point division is safer for display.
2. The teacher wants a boundary test at exactly 45 for the corrected program. Explain what input set would produce exactly 45 and why the current condition still passes or fails correctly.
Read the worked answer
For the corrected program, six marks of 45 give a total of 270 and an average of 270 / 6 = 45 exactly. That is a valid boundary test. With the condition average >= 45, the program should print First Division, not Pass. The >= is correct because 45 belongs to the First Division range. If the code used > 45 instead, the same input would fall through to Pass and the test would reveal the off-by-one error. A useful extra observation is that the current buggy program does not actually test this boundary: six marks of 45 give 270 / 5 = 54, so it prints First Division without ever evaluating exactly 45. Fixing the divisor is necessary before the boundary test is meaningful.
3. Suggest one validation check and one structural improvement to make the program reliable for any number of subjects.
Read the worked answer
Validation: before adding a mark, reject any value below 0 or above 100, and reject non-numeric input. This prevents impossible marks from silently changing the average. Also check that at least one subject has been entered before dividing. Structural improvement: store the marks in a list and divide by len(marks) instead of a fixed number. Then the same program works for five, six or eight subjects. The grade logic can be placed in a function that receives the average and returns the grade, which makes each part easier to test. These changes do not replace testing; they reduce the chance that a hidden constant produces a wrong result.