Skip to main content
← Advanced CS Study Guide

Code Scholars Tutoring · Advanced CS

0.3 Testing and Debugging Workflow

How it works

A test plan is written from the specification, not from the implementation, so it can catch what the implementation got wrong.

Write the empty, single, typical, and boundary cases before the code, then run them after.

Common error

Testing only the path you had in mind while coding, which confirms your assumptions rather than checking them.

Worked example

Java snippets run inside a main method. Add import java.util.*; when collection types are used.

Java

int[] values = {-4, -1, -9};
int best = values[0];
for (int value : values) best = Math.max(best, value);
System.out.println(best);

Output

-1

A negative-only test distinguishes a correct maximum implementation from one incorrectly initialized to zero. Record expected and observed results, isolate the cause, and rerun both the failing test and earlier successful cases.

Exercises

0.3.1 · Trace

Determine the exact output and explain the state changes.

Java

int[] a = {1, 2, 3};
int sum = 0;
for (int i = 0; i < a.length - 1; i++) sum += a[i];
System.out.println(sum);
Show solution

Output

3

The final index is excluded. A full traversal should use i < a.length.

0.3.2 · Explain

Give boundary tests for a method counting positive array elements.

Show solution

Test an empty array, a single positive value, a single zero, all negatives, and mixed values. Expected counts include 0 for empty and zero-only input.

Open Java compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic