Back to Student Worksheet
CS Grade 4-5 Answer Key

CS: Debugging: Finding and Fixing Errors

Practice spotting mistakes and improving simple programs

Answer Key
Name:
Date:
Score: / 15

CS: Debugging: Finding and Fixing Errors

Practice spotting mistakes and improving simple programs

CS - Grade 4-5

Instructions: Read each problem carefully. Find the error, explain what is wrong, and write a fix when asked.
  1. 1

    A program is supposed to print Hello, Sam! The code says: print("Hello, Sam!". Find the error and write the corrected code.

    Check that every opening symbol has a matching closing symbol.

    The code is missing a closing parenthesis. The corrected code is print("Hello, Sam!").
  2. 2

    A robot should move forward 3 spaces, turn right, and then move forward 2 spaces. The instructions are: move forward 3, turn left, move forward 2. Find the bug.

    The bug is that the robot turns left instead of right. The fixed instructions are move forward 3, turn right, and move forward 2.
  3. 3

    This program should count from 1 to 5, but it prints 1, 2, 3, 4. Code: for number in 1 to 4: print(number). What should be changed?

    Look at the last number the loop is allowed to use.

    The loop stops too early. It should count from 1 to 5, so the loop should use 1 to 5 instead of 1 to 4.
  4. 4

    A game gives 10 points when a player catches a star. The code says: score = score - 10. Explain the bug and fix it.

    The bug is that the code subtracts 10 points instead of adding 10 points. The fixed code is score = score + 10.
  5. 5

    A program checks if a student can enter a ride. The rule is: students must be 48 inches or taller. The code says: if height > 48 then enter. What is the problem?

    The words or taller include the exact height of 48 inches.

    The code does not allow a student who is exactly 48 inches tall to enter. The condition should be if height >= 48 then enter.
  6. 6

    Look at this algorithm for making a peanut butter sandwich: 1. Put peanut butter on bread. 2. Open the jar. 3. Get bread. 4. Eat sandwich. What sequencing bug do you see?

    The steps are in the wrong order. A better order is get bread, open the jar, put peanut butter on bread, and then eat the sandwich.
  7. 7

    A drawing program should draw a square. The instructions are repeated 4 times: move forward 50, turn right 90. A student changes it to repeat 3 times. What will happen?

    A square has four equal sides.

    The program will not finish the square because it only draws three sides. The repeat should be changed back to 4 times.
  8. 8

    A program asks for a name and then says hello. Code: name = input("What is your name?") print("Hello, name"). What is the bug?

    The program prints the word name instead of the value stored in the variable. It should print Hello plus the variable name, such as print("Hello, " + name).
  9. 9

    A student tests a calculator program with 2 + 3 and gets 5. They say the program has no bugs. Explain why more testing is needed.

    A bug might only appear with some inputs.

    One correct test does not prove the whole program works. The student should test more examples, such as larger numbers, zero, and different operations.
  10. 10

    A program should turn on a light when it is dark. The code says: if dark = false then turn on light. Find the logic error.

    The condition is backwards. The light should turn on when dark is true, so the code should say if dark = true then turn on light.
  11. 11

    A loop should play a sound 5 times. The code says: repeat 5 times: play sound, play sound. How many times will the sound play, and what is the bug?

    Count how many sound commands happen during each repeat.

    The sound will play 10 times because there are two play sound commands inside a loop that repeats 5 times. One play sound command should be removed.
  12. 12

    A spelling program should accept the word cat. The code checks if the word is equal to cot. What kind of bug is this, and how can it be fixed?

    This is a data or typing bug because the program checks the wrong word. The code should check if the word is equal to cat.
  13. 13

    A robot should reach the treasure by moving east 2 spaces and north 1 space. The code is: move east 1, move north 1. Find the missing step.

    Compare each instruction to the goal path.

    The robot is missing one move east step. The fixed code is move east 2 spaces and then move north 1 space.
  14. 14

    A program should print every even number from 2 to 10. It prints 2, 4, 6, 8, 11. What is the output bug?

    The last number is wrong because 11 is not even and is not between 2 and 10 as an even number. The last number should be 10.
  15. 15

    A classmate says, "Debugging means deleting all the code and starting over." Write a better explanation of debugging.

    Think about how a detective finds clues instead of guessing.

    Debugging means finding errors in a program, understanding why they happen, and fixing them. It usually involves checking small parts of the code and testing again.
LivePhysics™.com CS - Grade 4-5 - Answer Key