Finding and fixing bugs in a program

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Finding and fixing bugs in a program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Your program crashes with an error message. What should you do first?
A Read the error message and find the line it points to
B Delete the whole program and start again
2. Which is the better way to fix a program with three bugs?
A Fix one bug, then rerun the program to retest
B Change all three lines at once, then run it
3. Your program has a missing bracket (syntax error) and also adds when it should subtract (logic error). Which do you fix first?
A The logic error, because it is harder
B The syntax error, because the program cannot run until it is fixed
4. A program runs with no error message but shows 15 when you expected 50. What kind of bug is this?
A A syntax error: the computer would have shown a message
B A logic error: compare the output with what you expected
5. You fixed a bug and the error message disappeared. What should you do next?
A Run the program again and check the output is correct
B Assume it works and stop testing

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1console.log(total); // ReferenceError: total is not defined ADeclaring score twice with let causes a syntax error. Fix this one line (score = score + 5;) and then rerun.
2console.log(add(2, 3)); // shows 6, expected 5 Bfunction double(n) { return n + 2; } // fix to n * 2 console.log(double(5)); // retest: expect 10
3Fix syntax errors first, because a missing bracket stops the program running at all, and then look for logic errors. CThe error message names the problem and the line number, so you read it first, find that line, fix one thing and rerun.
4Check whether the bug is inside a function or in the main program, then fix one thing and retest by calling the function again. Dconsole.log("Hi"; // fix to: console.log("Hi");
5let score = 10; let score = score + 5; EA logic error: the program runs but the output does not match what you expected, so you check the function's code and fix it.
6function average(a, b) { return a + b / 2; } FA logic error: it runs but gives wrong answers because of the order of operations. Fix it to (a + b) / 2, then retest with known values.

Definitions

debugging

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let total = 0; ___________("total is", total);
2.
function area(w, h) { return w _ h; }
3.
function double(n) { return n * 2; } console.log(______(4)); // expected 8
4.
let score = 10; if (score ___ 10) console.log("Full marks");
5.
function add(a, b) { ______ a + b; }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let score = 10;
B console.log(score;
C score = score + 5;
D console.log(score);
Question 2
A fuction double(n) {
B return n * 2;
C }
D console.log(double(4));
Question 3
A let price = 20;
B let qty = 3;
C // expected total: 60
D let total = price + qty;
E console.log(total);
Question 4
A let count = 1;
B for (let i = 0; i < 5; i++) {
C count++;
D }
E console.log(count);
Question 5
A function greet(name) {
B return 'Hi ' + name;
C }
D let msg = Greet('Sam');
E console.log(msg);

Answer Key

Finding and fixing bugs in a program
Section A: Quick Questions
Q1
A — Read the error message and find the line it points to
Q2
A — Fix one bug, then rerun the program to retest
Q3
B — The syntax error, because the program cannot run until it is fixed
Q4
B — A logic error: compare the output with what you expected
Q5
A — Run the program again and check the output is correct
Section B: Match the Pairs
Q1
console.log(total); // ReferenceError: total is not defined → The error message names the problem and the line number, so you read it first, find that line, fix one thing and rerun.
Q2
console.log(add(2, 3)); // shows 6, expected 5 → A logic error: the program runs but the output does not match what you expected, so you check the function's code and fix it.
Q3
Fix syntax errors first, because a missing bracket stops the program running at all, and then look for logic errors. → console.log("Hi"; // fix to: console.log("Hi");
Q4
Check whether the bug is inside a function or in the main program, then fix one thing and retest by calling the function again. → function double(n) { return n + 2; } // fix to n * 2 console.log(double(5)); // retest: expect 10
Q5
let score = 10; let score = score + 5; → Declaring score twice with let causes a syntax error. Fix this one line (score = score + 5;) and then rerun.
Q6
function average(a, b) { return a + b / 2; } → A logic error: it runs but gives wrong answers because of the order of operations. Fix it to (a + b) / 2, then retest with known values.
Section C: Definitions
Q1
debugging — Finding and fixing bugs: run the program, read the error or compare wrong output with what you expected, find the faulty line, fix one thing, then rerun to retest.
Section D: Fill in the Blanks
Q1
let total = 0; console.log("total is", total);
Q2
function area(w, h) { return w * h; }
Q3
function double(n) { return n * 2; } console.log(double(4)); // expected 8
Q4
let score = 10; if (score === 10) console.log("Full marks");
Q5
function add(a, b) { return a + b; }
Section E: Spot the Error
Q1
B — Syntax error: the bracket is never closed. Fix syntax errors first. The line should be console.log(score);
Q2
A — Syntax error: the keyword is misspelled. JavaScript reads 'function' only when spelled exactly. Fix it, then rerun.
Q3
D — Logic error: the program runs but prints 23 instead of the expected 60. The total should be price * qty.
Q4
A — Logic error: the counter starts at 1, so the loop prints 6 instead of the expected 5. It should start at 0.
Q5
D — The bug is in the main program, not the function. JavaScript is case sensitive, so Greet is not defined (ReferenceError). Use greet.