CSC1110 Exercise 6

Using Chat-GPT To Debug Code

Chat-GPT is an amazing tool that being used to generate content of all types, from form letters to meeting agendas, essays and even source code. There is only one small problem with this: it doesn’t know what it’s doing.

Chat-GPT was trained on millions of lines of source code, among other things, and has seen a lot of good, well-designed code. It has also seen a lot of student code. The end result is a powerful system that can often be a great help to programmers when writing and debugging code, but will always appear confident, even when the answers it gives are wholly or partially incorrect.

In this exercise, you will use Chat-GPT to assist you in debugging some code. For each problem:

  1. Read the problem description so you know what the code should do.
  2. The code you are given will not be correct
    1. Trace it and see what you think the output will be.
    2. Try and fix the bugs in the code yourself.
  3. Enter the given prompt and buggy code into Chat-GPT
  4. Answer the following question: Did Chat-GPT give you the correct answer? If not, what is incorrect about it’s response? Be specific, include code generated by Chat-GPT in your answer.


  1. Write a method countToTen that will use a for loop to count from 1 to 10, incrementing a local variable ten inside the loop, then return ten. Here is a buggy version of the method:
public int countToTen() {
    int ten = 0;
    for(int i = 0; i < 10; i +=2) {
    return ten;

Use the following prompt in Chat-GPT:

is there anything wrong with the following java code: [paste code here]
  1. Write a method called middleChar that will get a String passed in as a parameter, get the number of characters in the String, and return the middle character of the String. If the String has an even number of letters, it should return the first of the two middle characters (i.e. for the word fluffy, the method will return ‘u’.
public char middleChar(String s) {
    int length = s.length();
    int middle = length / 2;
    return s.substring(0, middle);

Use the following prompt in Chat-GPT:

debug this java method: [paste code here]
  1. Write the equals method for the String class. Below is a buggy implementation.
public boolean equals(String s) {
    if(this == s) { return true; }
    if(!(s instanceof String)) { return false; }
    for(int i = 0; i < s.length(); ++i) {
        if(this.charAt(i) != s.charAt(i)) { return false; }
    return true;

Use the following prompt in Chat-GPT:

what is wrong with this java code? [paste code here]
  1. Try and rewrite the prompts to explain to Chat-GPT what your code is attempting to do, then submit the new prompt with the same buggy code. Did Chat-GPT perform better? Worse? The same? Explain your answers with code examples.

Submission Instructions:

Submit a PDF with your answers. For each question, show your trace of the code, your attempt at fixing the code, the complete output that Chat-GPT gave you and your thoughts on the output that Chat-GPT gave you.