Coding Sheets, Practice & Learning Support: Mistake-Based Coding Practice Sheets (Copy)
Mistake-Based Coding Practice Sheets
How To Use These Sheets (Fast Method)
- For each task:
- Identify the bug type
- State why it is wrong (logic reason)
- Write the minimum fix
- Run test data mentally using the provided trace table format
Sheet 1: Loop Mistakes (FOR / WHILE / REPEAT UNTIL)
Task 1: Off-By-One In FOR Loop
Faulty Pseudocode
total ← 0
FOR i ← 1 TO 10
INPUT num
total ← total + num
NEXT i
OUTPUT total / 9
- Do:
- Identify the mistake
- Fix it
- Give one test case where the wrong version fails
Test Data Suggestion
- Inputs: 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
Task 2: Infinite WHILE Loop (Missing Update)
Faulty Pseudocode
i ← 1
WHILE i <= 5 DO
OUTPUT i
ENDWHILE
- Do:
- Identify why it never stops
- Fix it
Task 3: Wrong STEP Direction In FOR Loop
Faulty Pseudocode
FOR i ← 10 TO 1 STEP 1
OUTPUT i
NEXT i
- Do:
- Identify the issue
- Fix it to count down properly
Task 4: REPEAT UNTIL Condition Reversed
Faulty Pseudocode
REPEAT
INPUT mark
UNTIL mark < 0 OR mark > 100
OUTPUT mark
- Do:
- Explain why this accepts invalid data
- Rewrite the UNTIL condition correctly
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Task 5: Validation Loop With Wrong Operator (Impossible Condition)
Faulty Pseudocode
INPUT age
WHILE age < 18 AND age > 60 DO
INPUT age
ENDWHILE
OUTPUT age
- Do:
- Identify why the loop may never run even for invalid age
- Fix the condition
Task 6: Loop Runs One Time Too Many (Array Out Of Range)
Faulty Pseudocode
DECLARE arr : ARRAY[1..5] OF INTEGER
FOR i ← 1 TO 6
INPUT arr[i]
NEXT i
- Do:
- Identify the exact out-of-range access
- Fix bounds
Sheet 2: IF / ELSE Logic Mistakes
Task 7: Pass/Fail Condition Reversed
Faulty Pseudocode
INPUT mark
IF mark < 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
- Do:
- Correct the condition
- Give 2 test values: one pass, one fail
Task 8: ELSEIF Order Makes A Grade Unreachable
Faulty Pseudocode
INPUT score
IF score >= 50 THEN
OUTPUT "C"
ELSEIF score >= 80 THEN
OUTPUT "A"
ELSE
OUTPUT "F"
ENDIF
- Do:
- Explain why “A” never prints
- Fix the ordering
Task 9: Condition Always TRUE
Faulty Pseudocode
INPUT x
IF x >= 0 OR x <= 100 THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
- Do:
- Explain why it always outputs “Valid”
- Fix the logic
Task 10: Missing ELSE Causes Logic Gap
Faulty Pseudocode
INPUT temp
IF temp > 40 THEN
OUTPUT "Hot"
ENDIF
- Do:
- Explain what output happens when temp = 20
- Rewrite with a complete decision structure
Sheet 3: Array Mistakes (1D + 2D)
Task 11: Wrong Index Used (Adds Same Element Repeatedly)
Faulty Pseudocode
sum ← 0
FOR i ← 1 TO n
sum ← sum + arr[1]
NEXT i
OUTPUT sum
- Do:
- Identify bug
- Fix it
Task 12: Max Initialised Incorrectly (Fails For All Negative)
Faulty Pseudocode
max ← 0
FOR i ← 1 TO n
IF arr[i] > max THEN
max ← arr[i]
ENDIF
NEXT i
OUTPUT max
- Do:
- Explain failure case
- Provide safe max algorithm pattern
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Task 13: 2D Array Swapped Indices
Faulty Pseudocode
FOR row ← 1 TO r
FOR col ← 1 TO c
total ← total + table[col, row]
NEXT col
NEXT row
- Do:
- Explain what’s wrong
- Fix the indexing
Task 14: Accumulator Not Reset For Each Row
Faulty Pseudocode
rowTotal ← 0
FOR row ← 1 TO r
FOR col ← 1 TO c
rowTotal ← rowTotal + table[row, col]
NEXT col
OUTPUT rowTotal
NEXT row
- Do:
- Explain why totals keep increasing
- Fix it so each row total is separate
Sheet 4: String Mistakes (Character-By-Character)
Task 15: Trying To Modify A String Directly
Faulty Pseudocode
INPUT text
text[1] ← "A"
OUTPUT text
- Do:
- Explain why this is invalid logic
- Fix by rebuilding the string
Task 16: Out-Of-Range Last Character Access
Faulty Pseudocode
INPUT word
last ← word[LENGTH(word) + 1]
OUTPUT last
- Do:
- Identify the exact index error
- Fix it
Task 17: Wrong Loop Bound For String Traversal
Faulty Pseudocode
INPUT text
FOR i ← 1 TO n
OUTPUT text[i]
NEXT i
- Do:
- Identify what should replace
n - Fix it properly
- Identify what should replace
Sheet 5: Searching / Sorting Mistakes
Task 18: Linear Search Never Moves Index (Infinite Loop)
Faulty Pseudocode
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ENDIF
ENDWHILE
- Do:
- Explain why it can loop forever
- Fix it using safe search structure
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Task 19: Sentinel Included In Total (Wrong Order)
Faulty Pseudocode
total ← 0
REPEAT
INPUT num
total ← total + num
UNTIL num = -1
OUTPUT total
- Do:
- Explain why sentinel is included
- Fix it so -1 is not added
Task 20: Bubble Sort Out Of Range
Faulty Pseudocode
FOR pass ← 1 TO n - 1
FOR j ← 1 TO n
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
NEXT pass
- Do:
- Identify out-of-range access
- Fix inner loop bounds correctly
Task 21: Bubble Sort Wrong Comparison For Descending
Faulty Pseudocode (intended descending)
FOR pass ← 1 TO n - 1
FOR j ← 1 TO n - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
NEXT pass
- Do:
- Explain why this sorts ascending
- Fix the IF condition for descending
Sheet 6: Flags (State Mistakes)
Task 22: Flag Reset Inside Loop (Destroys Meaning)
Faulty Pseudocode
FOR i ← 1 TO n
found ← FALSE
IF arr[i] = target THEN
found ← TRUE
ENDIF
NEXT i
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
- Do:
- Explain why final result depends only on last element
- Fix flag placement
Task 23: Swapped Flag Not Reset Each Pass (Bubble Sort Optimisation Bug)
Faulty Pseudocode
swapped ← TRUE
pass ← 1
WHILE pass <= n - 1 AND swapped = TRUE DO
FOR j ← 1 TO n - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
swapped ← TRUE
ENDIF
NEXT j
pass ← pass + 1
ENDWHILE
- Do:
- Explain why loop may never stop early
- Fix by resetting swapped correctly
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Trace Table Templates (Use For Any Task)
Trace Table: Loop + Variables
| Step / Iteration | Key Variable(s) | Condition Result | Output |
|---|
Trace Table: Array Traversal
| i | arr[i] | Important Variables (total/max/count/found) | Output |
|---|
Trace Table: String Traversal
| i | text[i] | result (built string) | counters/flags | Output |
|---|
ANSWER KEY (Fixes + Reasoning + Clean Correct Versions)
Task 1: Fix
- Error:
- Dividing by 9 when 10 inputs were summed
- Fix:
OUTPUT total / 10
Task 2: Fix
- Error:
inever changes
- Fix:
i ← 1
WHILE i <= 5 DO
OUTPUT i
i ← i + 1
ENDWHILE
Task 3: Fix
- Error:
- Counting down needs
STEP -1
- Counting down needs
- Fix:
FOR i ← 10 TO 1 STEP -1
OUTPUT i
NEXT i
Task 4: Fix
- Error:
- UNTIL condition is written for invalid data
- Fix:
REPEAT
INPUT mark
UNTIL mark >= 0 AND mark <= 100
OUTPUT mark
Task 5: Fix
- Error:
age < 18 AND age > 60is impossible
- Fix:
INPUT age
WHILE age < 18 OR age > 60 DO
INPUT age
ENDWHILE
OUTPUT age
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Task 6: Fix
- Error:
i = 6causesarr[6]out of range
- Fix:
DECLARE arr : ARRAY[1..5] OF INTEGER
FOR i ← 1 TO 5
INPUT arr[i]
NEXT i
Task 7: Fix
- Error:
- Pass/fail reversed
- Fix:
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Task 8: Fix
- Error:
score >= 50catches everything including 80+
- Fix:
INPUT score
IF score >= 80 THEN
OUTPUT "A"
ELSEIF score >= 50 THEN
OUTPUT "C"
ELSE
OUTPUT "F"
ENDIF
Task 9: Fix
- Error:
x >= 0 OR x <= 100always TRUE
- Fix:
INPUT x
IF x >= 0 AND x <= 100 THEN
OUTPUT "Valid"
ELSE
OUTPUT "Invalid"
ENDIF
Task 10: Fix
- Logic gap:
- No output when temp <= 40
- Fix:
INPUT temp
IF temp > 40 THEN
OUTPUT "Hot"
ELSE
OUTPUT "Not Hot"
ENDIF
Task 11: Fix
- Error:
- Adds
arr[1]repeatedly
- Adds
- Fix:
sum ← 0
FOR i ← 1 TO n
sum ← sum + arr[i]
NEXT i
OUTPUT sum
Task 12: Fix (Safe Max)
- Error:
max ← 0fails if all values negative
- Fix:
max ← arr[1]
FOR i ← 2 TO n
IF arr[i] > max THEN
max ← arr[i]
ENDIF
NEXT i
OUTPUT max
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Task 13: Fix
- Error:
- Using
table[col, row]swaps meaning
- Using
- Fix:
FOR row ← 1 TO r
FOR col ← 1 TO c
total ← total + table[row, col]
NEXT col
NEXT row
Task 14: Fix
- Error:
rowTotalmust reset each row
- Fix:
FOR row ← 1 TO r
rowTotal ← 0
FOR col ← 1 TO c
rowTotal ← rowTotal + table[row, col]
NEXT col
OUTPUT rowTotal
NEXT row
Task 15: Fix (Rebuild String)
- Error:
- Direct character assignment to string
- Fix:
INPUT text
newText ← ""
FOR i ← 1 TO LENGTH(text)
IF i = 1 THEN
newText ← newText & "A"
ELSE
newText ← newText & text[i]
ENDIF
NEXT i
text ← newText
OUTPUT text
Task 16: Fix
- Error:
- last index is
LENGTH(word)not+ 1
- last index is
- Fix:
INPUT word
last ← word[LENGTH(word)]
OUTPUT last
Task 17: Fix
- Error:
- Wrong loop bound variable
- Fix:
INPUT text
FOR i ← 1 TO LENGTH(text)
OUTPUT text[i]
NEXT i
Task 18: Fix (Safe Linear Search)
- Error:
- index never changes when not found
- Fix:
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
Task 19: Fix (Exclude Sentinel)
- Error:
- Adds -1 before stopping
- Fix:
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
Task 20: Fix (Bubble Sort Bounds)
- Error:
j = ncausesarr[n+1]
- Fix:
FOR pass ← 1 TO n - 1
FOR j ← 1 TO n - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
NEXT pass
Task 21: Fix (Descending)
- Error:
- Uses ascending comparison
- Fix:
FOR pass ← 1 TO n - 1
FOR j ← 1 TO n - pass
IF arr[j] < arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
NEXT pass
Task 22: Fix (Flag Outside Loop)
- Error:
- Resetting flag each iteration
- Fix:
found ← FALSE
FOR i ← 1 TO n
IF arr[i] = target THEN
found ← TRUE
ENDIF
NEXT i
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Task 23: Fix (Reset swapped Each Pass)
- Error:
swappednever reset to FALSE at start of pass
- Fix:
swapped ← TRUE
pass ← 1
WHILE pass <= n - 1 AND swapped = TRUE DO
swapped ← FALSE
FOR j ← 1 TO n - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
swapped ← TRUE
ENDIF
NEXT j
pass ← pass + 1
ENDWHILE
