Designing Algorithms, Dry Runs and Choosing Test Data (ISC Class 12 Computer Science)
Updated 18 October 2026 · Draft, not yet fact-checked
An algorithm is a finite sequence of clear, unambiguous steps that solves a problem for every valid input and stops. Design it by understanding inputs and outputs, splitting into sub-tasks, writing structured steps, checking by dry run on hand-worked examples, and only then coding. A dry run uses a trace table with a column for each variable; test data should include normal, boundary and special cases.
Code comes last: dry run your steps on normal, boundary and special data first, and go back to fix the steps whenever the trace table shows a wrong result.
Illustration drawn for QuestionBuddy.
Understand Designing Algorithms, Dry Runs and Choosing Test Data
1. Properties of an algorithm. Finiteness (it ends), definiteness (each step is precise), input (zero or more), output (at least one) and effectiveness (each step can be carried out).
2. Design steps
1. Understand the problem: identify inputs, outputs and the rule connecting them; work two or three examples by hand, including edge cases (0, 1, single digit, empty string).
2. Break it into sub-tasks, each becoming a method (accept, isPrime, reverse, check).
3. Write the steps in structured English or pseudocode using sequence, selection and iteration (or recursion).
4. Check correctness on the hand-worked examples by a dry run.
5. Only then write the Java code.
3. Dry run with a trace table. One column per variable and one row per iteration or call. Update the variables in the order the statements execute.
Example: Euclid's algorithm for a = 96, b = 36.
| a | b | r = a % b |
|---|---|---|
| 96 | 36 | 24 |
| 36 | 24 | 12 |
| 24 | 12 | 0 |
| 12 | 0 | stop |
GCD = 12. LCM = 96 / 12 x 36 = 8 x 36 = 288.
Reverse a number (3829).
| t | rev |
|---|---|
| 3829 | 9 |
| 382 | 92 |
| 38 | 928 |
| 3 | 9283 |
| 0 | stop |
4. Primality by trial division. Test d = 2, 3, ... while d x d <= x. For 29: d = 2 (29 % 2 = 1), 3 (2), 4 (1), 5 (4); d = 6 gives 36 > 29, so stop: prime. For 91: d = 2 (1), 3 (1), 4 (3), 5 (1), 6 (1), 7 (0): divisible by 7, so 91 is not prime.
5. Choosing test data
- Normal cases: typical values.
- Boundary cases: smallest and largest valid values (0, 1, one-element array, empty string).
- Special cases suggested by the definition (a palindromic prime for an Emirp check).
- Invalid input handling if the question asks.
6. Why Euclid's loop stops. GCD(a, b) = GCD(b, a mod b), and the remainder is always smaller than b, so b strictly decreases and must reach 0.
7. Loop errors to watch. Most are off-by-one: one iteration too many or too few, or a missing update that makes an infinite loop. Check the initial value, the condition and the update.
Designing Algorithms, Dry Runs and Choosing Test Data: key facts
- Properties of an algorithm
- Finiteness
- Definiteness
- Input
- Output
- Effectiveness
- Five.
- Design method
- Understand
- Split into sub-tasks
- Write steps
- Dry run
- Code
- Do not start with code.
- Test data
- Normal
- Boundary
- Special
- Invalid
- Cover every branch.
How to answer Designing Algorithms, Dry Runs and Choosing Test Data questions
Questions ask you to write an algorithm, trace given code, or propose test data.
- 1Write the inputs and outputs, then one worked example by hand.
- 2Draw a trace table with one column per variable and update in execution order.
- 3Stop the trace when the loop condition fails and state the result.
- 4For test data, name a value for each branch of the condition and say why.
Designing Algorithms, Dry Runs and Choosing Test Data in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Algorithm as a recipe
Suppose a canteen cook follows a card: boil 2 litres of water, add 3 spoons of tea, stop heating after 5 minutes, strain and serve.
Which properties of an algorithm does the card show?
Try it first, then show the solutionSolution
- Each step is precise: definiteness.
- It has an end: finiteness; the output is a cup of tea.
Answer: Definiteness, finiteness, input (water, tea) and output (tea).
Memory hook: A recipe is a human algorithm.
Dry run catches an error
Imagine a clerk tests a digit-counting rule on 4075 by hand before coding it, and finds the count is one short.
What did the dry run reveal and when was it done?
Try it first, then show the solutionSolution
- A logical error (for example a loop condition n > 9 that skips the last digit).
- It was found before coding, which is the point of a dry run.
Answer: A logical off-by-one error, found before coding.
Memory hook: Trace before you type.
Tricks to remember Designing Algorithms, Dry Runs and Choosing Test Data
Short memory aids. Say each one aloud once, then use it on a question the same day.
FDIOE
Finiteness, Definiteness, Input, Output, Effectiveness.
Use it when: Properties of an algorithm.
N-B-S-I
Normal, Boundary, Special, Invalid test data.
Use it when: Choosing test values.
Designing Algorithms, Dry Runs and Choosing Test Data: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Property of an algorithm · 1 mark · MCQBoard level
An algorithm whose steps eventually come to an end satisfies the property of:
(a) definiteness
(b) finiteness
(c) effectiveness
(d) input
Try it first, then show the solutionSolution
- Ending after a limited number of steps is finiteness.
Answer: (b)
Example 2: Euclid's trace · 2 marks · Short answerBoard level
Trace the loop 'while (b != 0) { r = a % b; a = b; b = r; }' for a = 96 and b = 36. Give the GCD and the LCM.
Try it first, then show the solutionSolution
- 96 % 36 = 24, so a = 36, b = 24; 36 % 24 = 12, so a = 24, b = 12; 24 % 12 = 0, so a = 12, b = 0.
- GCD = 12; LCM = 96 / 12 x 36 = 288.
Answer: GCD 12; LCM 288.
Example 3: Trace a reverse · 4 marks · Short answerHarder board level
Trace the loop 'rev = 0; t = 3829; while (t > 0) { rev = rev * 10 + t % 10; t = t / 10; }' and give rev and the number of times the loop body runs. Then say what changes if t starts at 3800.
Try it first, then show the solutionSolution
- Pass 1: rev = 0 x 10 + 9 = 9, t = 382. Pass 2: rev = 92, t = 38. Pass 3: rev = 928, t = 3. Pass 4: rev = 9283, t = 0.
- The body runs 4 times; rev = 9283.
- For 3800: pass 1: rev = 0, t = 380; pass 2: rev = 0, t = 38; pass 3: rev = 8, t = 3; pass 4: rev = 83, t = 0.
- So rev = 83: the trailing zeros of the original become leading zeros and are lost.
Answer: rev = 9283 after 4 passes; for 3800, rev = 83.
Example 4: Prime test by dry run · 4 marks · Short answerHarder board level
A method checks divisors d = 2, 3, ... while d * d <= x. Dry-run it for x = 29 and x = 91, and say how many divisors are tested in each case.
Try it first, then show the solutionSolution
- x = 29: d = 2 (remainder 1), 3 (2), 4 (1), 5 (4); then d = 6 gives 36 > 29, so the loop stops with no divisor: prime. Four divisors tested.
- x = 91: d = 2 (1), 3 (1), 4 (3), 5 (1), 6 (1), 7 (0): divisible by 7, so not prime. Six divisors tested.
- d * d <= x is enough because a factor larger than the square root pairs with a smaller one.
Answer: 29 is prime (4 tests); 91 is not prime (6 tests, divisor 7).
Example 5: Design and test · 8 marks · Long answerHOTS / competency
Design an algorithm to find whether a number is a 'digit-sum divisible' number: the sum of its digits divides the number exactly (for example 18 since 1 + 8 = 9 and 18 / 9 = 2). Give the steps, a dry run for 18 and 25, and a set of test values with reasons.
Try it first, then show the solutionSolution
- Steps: read n; set s = 0 and t = n; while t > 0: s = s + t % 10, t = t / 10; if s != 0 and n % s == 0, print yes, else print no.
- Dry run for 18: t = 18: s = 8, t = 1; t = 1: s = 9, t = 0; n % s = 18 % 9 = 0: yes.
- Dry run for 25: s = 5 then 7; 25 % 7 = 4: no.
- Test values: 18 (normal, yes), 25 (normal, no), 7 (single digit: s = 7, 7 % 7 = 0, yes), 0 (boundary: s = 0, must avoid division by zero, so print no or reject), 1 (boundary: yes).
- The check s != 0 prevents an ArithmeticException for n = 0.
Answer: 18 yes; 25 no; test 18, 25, 7, 0 and 1, with the s != 0 guard.
Where students lose marks in Designing Algorithms, Dry Runs and Choosing Test Data
The slips examiners see most often, and what to write instead.
Mistake: Writing code instead of an algorithm when an algorithm is asked
An algorithm is in structured English or pseudocode.
Wrong working
for (int i = 1; i <= n; i++) s += i;
Right working
Step 1: read n. Step 2: set s = 0. Step 3: for each i from 1 to n add i to s. Step 4: print s.
Do this: Write numbered steps.
Usually costs: 1-2 marks.
Mistake: Trace table without all variables
A missing column hides errors.
Wrong working
Only the value of rev is shown.
Right working
Show t and rev (and any other changing variable) row by row.
Do this: One column per variable.
Usually costs: 1-2 marks.
Mistake: Test data only from the middle range
Boundary and special values catch errors.
Wrong working
Tests: 13, 17, 19.
Right working
Tests: 13, 23, 11 (palindromic prime), 1 (boundary).
Do this: Cover every branch.
Usually costs: 1 mark.
Exam tips
- Number the steps of an algorithm and avoid Java syntax unless code is asked.
- Show the final answer after the last row of the trace.
- Always name one boundary and one special test value.
- State why the loop stops.
Practice questions on Designing Algorithms, Dry Runs and Choosing Test Data
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · Very short answerBoard level
Which step of designing an algorithm comes just before writing the Java code?
Hint
Checking by hand.
Answer
Checking correctness by a dry run on hand-worked examples.
Q2 · 2 marks · Short answerBoard level
State any two properties of an algorithm.
Hint
Ends and precise.
Answer
Finiteness (it ends) and definiteness (each step is precise); also input, output and effectiveness.
Q3 · 2 marks · Short answerHarder board level
Why is a boundary value such as 0 or 1 an important test value for a method that checks prime numbers?
Hint
Edge of the definition.
Answer
Neither 0 nor 1 is prime, but a careless loop may report them prime; testing them checks the lower boundary of the condition.
Q4 · 4 marks · Short answerHarder board level
Trace 'a = 84, b = 36' through Euclid's loop, give the GCD, and compute the LCM.
Hint
Use remainders.
Answer
84 % 36 = 12, so a = 36, b = 12; 36 % 12 = 0, so a = 12, b = 0. GCD = 12. LCM = 84 / 12 x 36 = 7 x 36 = 252.
Q5 · 4 marks · Short answerHOTS / competency
A loop meant to sum the digits of n is written as 'while (n > 9)'. Find the sum it returns for n = 5703, the correct sum, and the fault.
Hint
The last digit is skipped.
Answer
With n > 9: n = 5703: add 3, n = 570; add 0, n = 57; add 7, n = 5; stop. Sum = 10. Correct sum = 5 + 7 + 0 + 3 = 15. The condition stops before the leading digit 5 is added; use n > 0.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What is a dry run?Show answer
Executing an algorithm by hand, recording variable values step by step.
2.Why does Euclid's loop stop?Show answer
The remainder is smaller than b, so b decreases to 0.
3.What is a boundary test?Show answer
A test at the smallest or largest valid value.
4.Why test only up to the square root in a prime check?Show answer
Any larger factor pairs with a smaller one already tested.
Designing Algorithms, Dry Runs and Choosing Test Data: frequently asked questions
Is a flowchart required in Section B?
No; the program must show the logic through clear names and comments.
Can an algorithm have zero input?
Yes, it may take zero or more inputs but gives at least one output.
Where do I read about debugging?
On the debugging and Section B pattern topic page.