Identifying Errors (Cambridge (CIE) O Level Computer Science): Revision Note

Exam code: 2210

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Identifying errors

Examiner Tips and Tricks

Cambridge IGCSE 0478 regularly assesses your ability to identify, describe, and fix syntax, logic, and runtime errors, especially using trace tables and dry runs. This page mirrors the question style and mark scheme language of Paper 2.

  • Designing algorithms is a skill that must be developed and when designing algorithms, mistakes and issues will occur

  • Trace tables can also help to find any kind of error in a program or algorithm

  • There are three main categories of errors that when designing algorithms a programmer must be able to identify & fix, they are:

    • Syntax errors

    • Logic errors

    • Runtime errors

Examiner Tips and Tricks

When describing an error:

  • Use “prevents the program from running” for syntax

  • Use “produces incorrect output” for logic

  • Use “causes the program to crash” for runtime

These are the exact phrases that earn marks in the exam.

Syntax Errors

What is a syntax error?

  • A syntax error is an error that breaks the grammatical rules of a programming language and stops it from running

  • Examples of syntax errors are:

    • Typos and spelling errors 

    • Missing or extra brackets or quotes

    • Misplaced or missing semicolons

    • Invalid variable or function names

    • Incorrect use of operators

    • Incorrectly nested loops & blocks of code

Python code - with syntax errors

def generate_username(first_name, last_name)
    username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"
    return username

def main():
  # -----------------------------------------------------------------------
  # Prompts the user for personal information and displays a suggested username
  # -----------------------------------------------------------------------
    first_name = imput("Enter your first name: ")
    last_name = input("Enter your last name: ")
    usdrname = generate_username(first_name, last_name)
    print(f"Here's a suggested username: {username}")

# ------------------------------------------------------------------------
# Main program
# ------------------------------------------------------------------------
main

Python code - without syntax errors

def generate_username(first_name, last_name):
    username = f"{first_name[0]}{last_name[0]}{last_name[:3]}"
    return username

def main():
  # -----------------------------------------------------------------------
  # Prompts the user for personal information and displays a suggested username
  # -----------------------------------------------------------------------
    first_name = input("Enter your first name: ")
    last_name = input("Enter your last name: ")
    username = generate_username(first_name, last_name)
    print(f"Here's a suggested username: {username}")

# ------------------------------------------------------------------------
# Main program
# ------------------------------------------------------------------------
main()

Syntax errors

  1. Missing semicolon def generate_username(first_name, last_name)

  2. Incorrectly spelt function name first_name = imput("Enter your first name: ")

  3. Typo in variable name usdrname = generate_username(first_name, last_name)

  4. Missing parenthesis main

Examiner Tips and Tricks

Examiners won’t give marks for vague phrases like “the program didn’t work.”
You must name the error type, locate it, and explain the impact on the output or execution.

Logic Errors

What is a logic error?

  • A logic error is where incorrect code is used that causes the program to run, but produces an incorrect output or result

  • Logic errors can be difficult to identify by the person who wrote the program, so one method of finding them is to use 'Trace Tables'

  • Examples of logic errors are:

    • Incorrect use of operators  (< and >)

    • Logical operator confusion (AND for OR)

    • Looping one extra time

    • Indexing arrays incorrectly (arrays indexing starts from 0)

    • Using variables before they are assigned

    • Infinite loops

Python code

def calculate_area(length, width):
    ###
    Calculates the area of a rectangle
    Inputs:
        length (float): The length of the rectangle (positive value)
        width (float): The width of the rectangle (positive value)
    Returns:
        float: The calculated area of the rectangle
    Raises:
        ValueError: If either length or width is non-positive
    ###
    if length < 0 or width < 0:
        raise ValueError("Length and width must be positive values.")
    area = length * width
    return area

def main():
    # -----------------------------------------------------------------------
    Prompts the user for rectangle dimensions and prints the calculated area
    # -----------------------------------------------------------------------
    try:
        length = float(input("Enter the length of the rectangle: "))
        width = float(input("Enter the width of the rectangle: "))
        # Call the area calculation function
        area = calculate_area(length, width)
        print(f"The area of the rectangle is approximately {area:.2f} square units.")
    except ValueError as error:
        print(f"Error: {error}")

# -----------------------------------------------------------------------
# Main program
# -----------------------------------------------------------------------
main()

Logic errors

Test number

Test data

Expected outcome

Actual outcome

Changes needed? (Y/N)

1

Length = 5

Width = 5

"The area of the rectangle is approximately 25 square units."

"The area of the rectangle is approximately 25.00 square units."

N

2

Length = 10

Width = 0

"Length and width must be positive values."

"The area of the rectangle is approximately 0.00 square units."

Y
should not accept 0 input as not positive

  • Logic error located on line if length < 0 or width < 0:

  • Logic error identified in expression < 0, should be <= 0 so that 0 is not accepted as valid input for length or width

Runtime Errors

What is a runtime error?

  • A runtime error is where an error causes a program to crash

  • Examples of runtime errors are:

    • Dividing a number by 0

    • Index out of the range of an array

    • Unable to read or write a drive

Python code

def calculate_area(length, width):
  """
  Calculates the area of a rectangle
  Inputs:
      length (float): The length of the rectangle (positive value)
      width (float): The width of the rectangle (positive value)
  Returns:
      float: The calculated area of the rectangle
  Raises:
      ValueError: If either length or width is non-positive
  """
  if length < 0 or width < 0:
    raise ValueError("Length and width must be positive values.")
  area = length * width
  return area

def main():
  # -----------------------------------------------------------------------
  Prompts the user for rectangle dimensions and prints the calculated area
  # -----------------------------------------------------------------------
  try:
    length = float(input("Enter the length of the rectangle: "))
    width = float(input("Enter the width of the rectangle: "))
    # Call the area calculation function
    area = calculate_area(length, width)
    print(f"The area of the rectangle is approximately {area:.2f} square units.")
  except ValueError as error:
    print(f"Error: {error}")

# -----------------------------------------------------------------------
# Main program
# -----------------------------------------------------------------------
main()

Runtime errors

Test number

Test data

Expected outcome

Actual outcome

Changes needed? (Y/N)

1

Length = 10

Width = 10

"The area of the rectangle is approximately 100 square units."

"The area of the rectangle is approximately 100.00 square units."

N

2

Length = "abc"

Width = 0

"Program could not convert string to float, try again"

Program crashed

Y
should give error message and ask user to enter again

  • Runtime error located in

try:
    length = float(input("Enter the length of the rectangle: "))
    width = float(input("Enter the width of the rectangle: "))
    # Call the area calculation function
    area = calculate_area(length, width)
    print(f"The area of the rectangle is approximately {area:.2f} square units.")
  except ValueError as error:
    print(f"Error: {error}")
  • Runtime error identified as missing iteration (while loop) so program does not ask user to enter width and height again

  • Corrected code now includes a while loop

while True:
  try:
      length = float(input("Enter the length of the rectangle: "))
      width = float(input("Enter the width of the rectangle: "))
      # Call the area calculation function
      area = calculate_area(length, width) 
      print(f"The area of the rectangle is approximately {area:.2f} square units.")
      break  # Exit the loop if successful
   except ValueError as error:
      print(f"Error: {error}")
      print("Please enter positive values for length and width.")

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.