Task 7 Debugging Practice Bank
Use these short tasks to practise before the programming practical test. For each task, identify the error type, explain the problem and write a correction.
Practice 1: Syntax Error
score <- 8
IF score >= 5
OUTPUT "Pass"
ENDIF
Model Answer
The error is a syntax error because the IF line is missing THEN.
score <- 8
IF score >= 5 THEN
OUTPUT "Pass"
ENDIF
Practice 2: Logical Error
total <- theoryMinutes - practiceMinutes
The program is meant to calculate total study minutes.
Model Answer
The error is a logical error because the program runs but calculates the wrong result. The statement should add the two values.
total <- theoryMinutes + practiceMinutes
Practice 3: Run-Time Error
average <- total / numberOfSessions
Model Answer
A run-time error could occur if numberOfSessions is 0 because division by zero is not valid. The program should validate that numberOfSessions is greater than 0 before calculating the average.
Practice 4: Boundary Error
IF totalMinutes > 30 AND totalMinutes < 60 THEN
feedback <- "Balanced"
ENDIF
The program should count totals from 30 to 60 minutes as balanced.
Model Answer
The error is a logical error. The boundary values 30 and 60 are incorrectly excluded.
IF totalMinutes >= 30 AND totalMinutes <= 60 THEN
feedback <- "Balanced"
ENDIF
Practice 5: Counter Error
balancedCount <- 0
FOR session <- 1 TO 3
INPUT totalMinutes
IF totalMinutes >= 30 AND totalMinutes <= 60 THEN
balancedCount <- 1
ENDIF
ENDFOR
Model Answer
The error is a logical error. The counter is reset to 1 each time a balanced session is found instead of increasing by 1.
balancedCount <- balancedCount + 1
Practice 6: Validation Gap
INPUT minutes
total <- total + minutes
Model Answer
The code accepts any value. If the user enters a negative number, the total becomes inaccurate. The program should check that minutes is not negative before adding it.
INPUT minutes
IF minutes >= 0 THEN
total <- total + minutes
ELSE
OUTPUT "Minutes cannot be negative"
ENDIF
Practice 7: Trace To Find The Error
total <- 0
FOR count <- 1 TO 3
INPUT minutes
total <- minutes
ENDFOR
OUTPUT total
Test data: 10, 15, 20.
| count | minutes | total |
|---|---|---|
| start | - | 0 |
| 1 | 10 | 10 |
| 2 | 15 | 15 |
| 3 | 20 | 20 |
Model Answer
The program only outputs the final input value. The error is in total <- minutes. It should accumulate:
total <- total + minutes
Practice 8: Documentation Fix
Weak comment:
// loop
Stronger comment:
// Repeat for three study sessions so the final count covers the whole study plan.
Good comments explain why the code exists or what important logic does. They do not repeat obvious code.