AS Level · Topic 12.3
A-Level Computer Science 9618 / Topic 12 / AS

Find the faults.

Every non-trivial program contains errors when it is first written. Professionals reduce how many they make, then use a planned mix of testing methods to expose the rest before users meet them. Learn the three types of error, how to locate and correct them, and when to use each testing method.

The key idea

A syntax error breaks the grammar rules of the language, so the translator cannot translate the code. A logic error is a mistake in the algorithm: the program runs but gives the wrong result. A run-time error happens while the program is running and makes it crash or hang, for example dividing by zero or using an array index outside its bounds. Faults are avoided by good design and programming practice and exposed by testing: dry runs, walkthroughs, white-box and black-box testing, stub and integration testing, then alpha, beta and acceptance testing.

Write codeTranslateRunOutput avoid faults syntax errorrun-time errorlogic error good design,clear names,IDE checks breaks the rulesof the language;not translated program crashesor hangs, e.g.divide by zero program runs butthe result iswrong the translator reports syntax errors; only testing and dry runs expose logic errors
Fig. 1: where each type of error shows itself as a program goes from source code to output
Section 01

Three types of error.

The type of an error depends on when and how it shows itself (Fig. 1). You must be able to locate an error in code, say which type it is and correct it.

TypeWhat it isFound byExamples
SyntaxA statement does not follow the rules of the languageThe compiler or interpreter (and IDE), before or as the code is translatedMisspelt keyword, missing ENDIF or NEXT, unmatched bracket, missing THEN
LogicThe algorithm is wrong; the program runs but the output is incorrectOnly by testing with known expected results, or a dry run> instead of >=, wrong loop limits, total not set to 0, wrong formula
Run-timeAn error during execution that the program cannot handle, so it crashes or hangsRunning the program with data that triggers itDivision by zero, array index out of range, reading past the end of a file, infinite loop
FUNCTION CountPasses(Marks : ARRAY[1:30] OF INTEGER) RETURNS INTEGER
    DECLARE Index, Passes : INTEGER
    Passes ← 0
    FOR Index ← 0 TO 30
        IF Marks[Index] > 50 THEN
            Passes ← Passes + 1
        ENDIF
    ENDFOR
    RETURN Passes
ENDFUNCTION
Worked example Locate, identify, correct
CountPasses should return how many of the 30 marks are passes. A pass is 50 or more. The code above contains three errors. Locate each, identify its type and correct it.
  1. ENDFOR is not a keyword: a FOR loop must end with NEXT. This is a syntax error; correct it to NEXT Index.
  2. FOR Index ← 0 TO 30 uses Marks[0], which is outside the array bounds 1 to 30. This crashes the program while it runs: a run-time error. Correct it to FOR Index ← 1 TO 30.
  3. Marks[Index] > 50 does not count a mark of exactly 50 as a pass. The program runs but returns the wrong count: a logic error. Correct it to Marks[Index] >= 50.
Answer: Syntax: ENDFOR to NEXT Index; run-time: loop from 1, not 0; logic: >= 50 instead of > 50
Examiner trap

A program that crashes is not a logic error. If it stops while running (divide by zero, index out of range), it is a run-time error; if it finishes but gives the wrong answer, it is a logic error.

Section 02

Exposing and avoiding faults.

Each program in the lab has one logic error. Run it, compare the output with what it should be, then find the line that is wrong: a dry run with a trace table is the quickest way.

Your turn
A program to find the average of N numbers crashes when N is 0. Identify the type of error and suggest how to avoid it.
Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

A program runs and finishes but outputs the wrong total. What type of error is this?

Quick check+10 XP

Which error is reported by the compiler or interpreter when it translates the code?

Quick check+10 XP

In the lab, which is the quickest way to find the wrong line in a program that runs but gives the wrong answer?

Quick check+10 XP

What is a stub?

Section 03

Methods of testing.

No single method finds every fault, so a test strategy uses several, at different points in development. Learn what each one is and when it is used.

MethodWhat it is
Dry runWorking through an algorithm or code by hand, recording variable values in a trace table, without running it on a computer.
WalkthroughThe programmer talks a group of peers through the design or code line by line, so others can spot errors.
White-boxTesting with knowledge of the code: test data is chosen to make every path through the code (every branch and loop) execute.
Black-boxTesting without looking at the code: inputs are chosen from the specification and the actual outputs are compared with the expected outputs.
StubA module not yet written is replaced by a simple dummy (stub) that, for example, outputs a message or returns a fixed value, so the modules that call it can be tested.
IntegrationModules that have been tested separately are combined and tested together, to check they work together and pass data correctly.
AlphaTesting of a nearly complete program by the developer's own staff (in-house), before it is released to anyone outside.
BetaA nearly finished version is released to a limited group of real users outside the company, who use it in normal conditions and report faults.
AcceptanceThe client or end users test the finished program to check it meets the requirements specification, before they accept it and sign it off.
// stubs used while the real modules are still being written
PROCEDURE OutputAverage(Total : INTEGER)
    OUTPUT "OutputAverage called, Total = ", Total
ENDPROCEDURE

FUNCTION GetMark() RETURNS INTEGER
    RETURN 75
ENDFUNCTION
Worked example Choose the method
Name the testing method in each case. (a) A games studio lets 2000 players download an unfinished version and report bugs. (b) A tester checks every branch of an IF statement is executed. (c) A school tests a new reports system before agreeing to pay for it.
  1. (a) Real users outside the company, before full release: beta testing.
  2. (b) Data chosen using knowledge of the code's paths: white-box testing.
  3. (c) The client checks it meets the requirements before signing off: acceptance testing.
Answer: (a) beta (b) white-box (c) acceptance
Examiner trap

Alpha and beta are not about version numbers. Alpha testing is done in-house by the developer's staff; beta testing is done by real users outside the company.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

A program uses Total / Count and stops with an error when Count is 0. What type of error is this?

Exam style+20 XP

Which describes black-box testing?

Exam style+20 XP

Which testing is carried out by a limited group of real users outside the developer's company?

Exam style+20 XP

Modules A and B each passed their own tests, but the program fails when A calls B. Which testing should have found this?

Skill unlocked

Errors and testing methods, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Syntax, logic and run-time errors, locating and correcting them, avoiding faults, and the main testing methods.