Chapter 2 · ICT Elective C

Program Testing & Debugging (II)

Master overflow errors, numerical errors, and software debugging tools to write robust, error-free Python programs.

Understand overflow & underflow errors
Learn truncation & rounding errors
Identify syntax, logic & run-time errors
Apply program traces & breakpoints

What You Will Learn

2.1 Types of Program Errors
Overflow, numerical, syntax, logic, run-time
2.2 Software Debugging Tools
Program traces, breakpoints, IDE tools
Chapter Summary
Mind map & key concepts
Interactive Quiz
12 questions with instant feedback
Flashcards
20 key terms with 3D flip
Past Questions
Exam practice with model answers

Types of Program Errors

A. Overflow & Underflow Errors

Overflow Error

Occurs when a stored number exceeds the maximum range of its data type. E.g., accessing an array index out of bounds, or a float result too large.

Underflow Error

Occurs when a number is lower than the minimum range of its data type. In Python, integers have no size restriction, but floats do.

Python Note In Python, there is NO restriction on the size of data stored in the integer data type. However, float has a maximum value (~1.8 × 10^308) and minimum positive value (~2.2 × 10^-308).
import sys print(sys.float_info.max) # Maximum float value print(sys.float_info.min) # Minimum float value import math e = math.e print(pow(e, 1000)) # OverflowError: Result too large

B. Numerical Errors

Truncation Errors

When there is insufficient memory to store a number, extra digits are cut off. The difference between the true value and the stored value is the truncation error.

Truncation error = | True value − Actual value stored |

Example: Saving π in a variable that stores 3 decimal places → stored as 3.141, truncation error = 0.000592653589...

import math num = 1234.56789 integer_part = math.trunc(num) print(integer_part) # 1234 # Alternative using int() integer_part = int(num) print(integer_part) # 1234

Rounding Errors

Rounding error = | True value − Rounded value |

Three methods of rounding:

Round Off (四捨五入)

If the (n+1)th decimal digit is ≤ 4, truncate. If ≥ 5, truncate and add 1 to the nth digit.

Example: Round off π to 2 decimal places = 3.14; to 4 decimal places = 3.1416

import math PI = math.pi print(PI) # 3.141592653589793 print(round(PI, 2)) # 3.14 print(round(PI, 4)) # 3.1416
Round Up (上捨)

Raise the number to the nearest value at the specified digit. Example: Round up π to 2 decimal places = 3.15

Misconception Round down and truncation are NOT the same for negative numbers. E.g., round down -3.456 to 2 d.p. = -3.46, but truncate -3.456 to 2 d.p. = -3.45.
Round Down (下捨)

Lower the number to the nearest value at the specified digit. Example: Round down π to 2 decimal places = 3.14

Comparison: Saving e (2.71828...) to 2 decimal places

Method Stored Value Error
Round off 2.72 0.001718171540955
Round up 2.72 0.001718171540955
Round down 2.71 0.008281828459045
Truncation 2.71 0.008281828459045
Rounding errors are always ≤ truncation errors. Python rounds floats by default.

C. Other Errors: Syntax, Logic & Run-Time

Syntax Errors Logical Errors Run-Time Errors
Definition Code cannot be compiled/executed — breaks language rules. Program runs but produces wrong results due to logic errors. Program stops unexpectedly during execution.
Examples Missing colons, misspelt keywords, wrong letter case, missing brackets Wrong sequence, errors in conditional structure, infinite loops Dividing by zero, square root of negative, array index out of range
Detection IDE detects automatically Test with test data IDE shows error message after program stops

Software Debugging Tools

After writing a program, test it systematically. If the test result does not match the expected result, the program contains errors (bugs). Debugging is the process of identifying and fixing these errors.

A. Tracing Variable Values (Program Trace)

Using the debug mode of the IDE, you can execute the program step by step and monitor the value of each variable. This method is called a program trace.

Echo Test (回聲測試) Using print() statements at key positions to display variable values is called an echo test. These lines don't affect program logic and can be removed after debugging.

B. Breakpoints

A breakpoint is a position where program execution is suspended. When paused, you can view the current values of variables to find errors.

How to Use Breakpoints in Thonny:

Example: Using Breakpoints to Debug

marks = [98, 79, 56, 68, 77, 93] avg_mark = sum(marks) / len(marks) if avg_mark > 80: grade = "Distinction" elif avg_mark > 50: grade = "Pass" # ← Breakpoint here else: grade = "Fail" print(avg_mark, grade)

When paused at the breakpoint: avg_mark = 78.5, marks = [98, 79, 56, 68, 77, 93]. The elif branch is executed because 78.5 > 50.

Key Insight If the program is paused at a breakpoint inside a branch, it means the condition is TRUE and that branch will execute. If the program skips the breakpoint, the condition is FALSE.

Key Concepts at a Glance

Types of Program Errors
Overflow Error / Underflow Error
Numerical Error → Truncation Error / Rounding Error (Round off, Round up, Round down)
Syntax Error / Logical Error / Run-Time Error
Debugging
Manual Methods: Test data, Flags, Stubs, Echo Test
Software Tools: Program Traces, Breakpoints (IDE)

Dijkstra's Insight

"If debugging is the process of removing software bugs, then programming must be the process of putting them in."

— Edsger Wybe Dijkstra

Debugging is a necessary and normal part of programming. Every programmer encounters bugs — persistence and systematic testing are key.

Test Your Knowledge

Answer all 12 questions, then click Submit to see your score.

Q1. Which error occurs when a stored number exceeds the maximum range of its data type?

A. Syntax error
B. Overflow error
C. Logic error
D. Truncation error

Q2. What is the truncation error when π (3.14159...) is stored to 2 decimal places?

A. 0.00159265...
B. 0.00840734...
C. 0.01
D. 0

Q3. In Python, which function is used to round off a number to n decimal places?

A. math.trunc()
B. int()
C. round()
D. math.floor()

Q4. Which type of error causes the program to produce wrong results without stopping?

A. Syntax error
B. Overflow error
C. Logical error
D. Run-time error

Q5. A breakpoint is added inside an if-branch. The program is paused there. What does this tell us?

A. The condition is FALSE
B. The condition is TRUE
C. There is a syntax error
D. The program has crashed

Q6. Which Python statement is used to check the maximum float value?

A. print(float.max)
B. print(sys.float_info.max)
C. print(math.maxfloat)
D. print(max(float))

Q7. Round off π to 4 decimal places. What is the result?

A. 3.1415
B. 3.1416
C. 3.1417
D. 3.142

Q8. Which of the following is an example of a run-time error?

A. Missing colon after if statement
B. An infinite loop
C. Dividing by zero
D. Wrong variable name spelling

Q9. What is the process of identifying and fixing bugs in a program called?

A. Compiling
B. Debugging
C. Testing
D. Tracing

Q10. In Python, what does int(3.99) return?

A. 4
B. 3.99
C. 3
D. Error

Q11. Which statement about rounding errors vs truncation errors is TRUE?

A. Rounding errors are always greater than truncation errors
B. Rounding errors are always less than or equal to truncation errors
C. They are always equal
D. There is no relationship between them

Q12. The echo test method uses which Python feature to display variable values?

A. input()
B. print()
C. debug()
D. trace()
0/12

Key Terms Flashcards

Click any card to reveal the definition. Click again to flip back.

Exam Practice

Filter by difficulty and click 'View Reference Answer' to see model answers.

Basic Chapter Exercise
What is the size of array num if Jeremy allocates 400 bytes and each float element uses 4 bytes? (1 mark)
Reference Answer:

Size = 400 ÷ 4 = 100 elements. The array num can store 100 floats.

Basic Chapter Exercise
Jeremy tried to access num[100] but failed. Which kind of error did he encounter? Explain briefly. (2 marks)
Reference Answer:

Overflow error. The array has 100 elements (index 0–99). Accessing index 100 exceeds the valid range of the array index, causing an overflow error.

Medium Chapter Exercise
Jeremy writes: a←2, b←3, c←√(a²+b²), num[1]←round(c,2), Output num[1]. (i) What error does this result contain? (ii) Find the value of the error to 10 significant figures. (2 marks)
Reference Answer:

(i) Rounding error. c = √(4+9) = √13 ≈ 3.605551275..., rounded to 2 d.p. = 3.61. (ii) Rounding error = |3.605551275... − 3.61| = 0.004448724...

Medium Chapter Exercise
Describe how breakpoints can help us spot errors in a program.
Reference Answer:

Breakpoints suspend program execution at a specified line. When paused, we can inspect the current values of all variables in the IDE. By checking whether these values are correct at that point, we can determine if the error occurred before or after the breakpoint. This helps narrow down the location of bugs efficiently.

Hard Chapter Exercise
Kelly writes an algorithm with array B of size 4. If D=6, trace through: i←0, while D>0: B[3-i]←D%2, D←D//2, i←i+1. What is the content of array B? What is the purpose of this algorithm?
Reference Answer:

Trace: D=6, i=0 → B[3]=6%2=0, D=3, i=1 → B[2]=3%2=1, D=1, i=2 → B[1]=1%2=1, D=0, i=3 → loop ends. B = [0,1,1,0] (index 0,1,2,3). Purpose: Convert a decimal integer D to its 4-bit binary representation stored in array B.

Hard Chapter Exercise
Fill in the trace table for the algorithm: step←10, total←0, for i from 1 to step: total←total+(i×i)+step. What is the output? (4 marks)
Reference Answer:

i=1: 1+10=11, total=11 | i=2: 4+10=14, total=25 | i=3: 9+10=19, total=44 | i=4: 16+10=26, total=70 | i=5: 25+10=35, total=105 | i=6: 36+10=46, total=151 | i=7: 49+10=59, total=210 | i=8: 64+10=74, total=284 | i=9: 81+10=91, total=375 | i=10: 100+10=110, total=485. Output: 485

Key Terms & Definitions