Java Programming Debugging part | Coursera Week 2 Quiz answers

In this post, you will get all weeks quiz answers of Java Programming Debugging part Coursera Week 2 Quiz answers

Course overview (All weeks quizzes answers link)

Java Programming Debugging part Coursera Week 2 Quiz answers

Practice Quiz: Debugging Part 1 Quiz Answers Coursera – 12 questions

Q1. Your friend is trying to solve the following problem using Java:

Write a method that finds each occurrence of “abc_” in a String input (where _ is a single character) and prints “bc_” for each such occurrence. For example, findAbc(“abcdefabcghi”) should print:

1. bcd
2. bcg

You friend has just finished writing a solution and needs help testing it.

Create a new BlueJ project. Create a class and copy and paste the two methods (below) into it.

public void findAbc(String input) {
    int index = input.indexOf("abc");
    while (true) {
        if (index == -1) {
            break;
        }
        String found = input.substring(index+1, index+4);
        System.out.println(found);
        index = input.indexOf("abc", index+4);
    }
}
   public void test() {
    //no code yet
}

Test method findAbc() by adding a line in the test() method:

 findAbc("abcd");

Do this example by hand. What should the output be if the method is working correctly?

Answer: Comment the CORRECT answer

Java Programming Debugging part Coursera Week 2 Quiz answers

Q2. Compile your class and run the test() method. What is the output?

Answer: bcd

Q3. So far so good! Comment out the line findAbc(“abcd”) and add a new line in the test() method:

findAbc("abcdabc");

What should the output be if the method is working correctly?

Answer: bcd

Q4. Compile and run your code. Which two of the following are results?

a) “bcd” is printed.
b) A StringIndexOutOfBoundsException is thrown.
c) A blank line is printed.
d) “Error” is printed.

Answer: a), b)

Q5. Which line of the program is causing the error?

a) int index = input.indexOf(“abc”);
b) if (index == -1) {
c) String found = input.substring(index+1, index+4);
d) index = input.indexOf(“abc”, index+4);

Answer: c) String found = input.substring(index+1, index+4);

Q6. What is the index that is out of range?

Answer: 8

Q7. What is the length of input for this method call?

Answer: 7

Q8. At the time this error is produced, what are the values of index+1 and index+4? You may want to add print statements to your code and run it again to see what these values are.

Enter your answer as numbers separated by a comma: int1, int2. For example if index+1 were 1 and index+4 were 5, you would enter:
1, 5

Answer: Comment the CORRECT answer

Q9. Why does the program throw an index out of bounds exception for this input?

a) The program finds the wrong index for the second occurrence of “abc”.
b) The method findAbc() never works when the String input is longer than 6 characters.
c) The method substring() is trying to access index 8 but the String input is only 7 characters long.
d) The method substring() is trying to access index 5, but the value of the int index is 4.

Answer: c)

Q10. Imagine your friend wants to get help from Coursera classmates on the discussion forums.

Which one of the following would be the most helpful way to describe the problem so that others can easily help?

a) My method findAbc() has an index out of bounds exception.
b) My method findAbc() has an index out of bounds exception when I call it with input “abcdabc”.
c) My method findAbc() has an index out of bounds exception when I call it with input “abcdabc”. The exception is at the line
String found = input.substring(index+1, index+4);
d) My method findAbc() has an index out of bounds exception at the line
String found = input.substring(index+1, index+4);
when I call it with input “abcdabc”. It prints “bcd” and then throws the exception. The index out of range is 8.

Answer: d)

Q11. Which of the following are examples of input that would also throw this exception? Check all that are correct.

a) “woiehabchi”
b) “abcbbbabcdddabc”
c) “aaaaabc”
d) “yabcyabc”
e) “eusabce”

Answer: b), c), d)

Q12. Add an if statement, such that the while loop breaks if the index is out of bounds. Compile the code and run it again. It should now print “bcd” without throwing an exception. Try it on the examples you selected the previous question to make sure it works.

What are possible conditions for this if statement? (Choose all that are correct.)

a) index >= input.length() – 3
b) index >= input.length() – 4
c) index > input.length() – 3
d) index > input.length() – 4

Answer: a), d)

Practice Quiz: Debugging Part 2 Quiz Answers Coursera – 11 questions

Q1. You have fixed one bug in your friend’s code, so that it no longer throws an index out of bounds exception, by adding the condition index >= input.length() – 3:

public void findAbc(String input){
       int index = input.indexOf("abc");
       while (true){
           if (index == -1 || index >= input.length() - 3){
               break;
           }
           String found = input.substring(index+1, index+4);
           System.out.println(found);
           index = input.indexOf("abc",index+4);
       }
   }

   public void test(){
       //findAbc("abcd");
       findAbc("abcdabc");
   }

Let’s test some more. Run the above code with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”. What is the output?

Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu

Answer: bcd, bca

Q2. Check this answer – do the problem by hand. What should be the correct output for input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”?

Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu

Answer: bcd, bca, bca

Q3. You will have to do some debugging, since the output wasn’t what you were expecting. Let’s see which occurrences of “abc” the program is finding. Add a line to print the index before found is calculated.

What are the indices printed? Select all that are correct.

a) 0
b) 1
c) 30
d) 31
e) 33

Answer: a), c)

Q4. You can see that the program is finding the first two occurrences of “abc” but not the third. The while loop is breaking without finding this occurrence. So we know that when the variable index is updated after finding the second occurrence of “abc” at index 30, it must be updated either to -1 or to something greater than or equal to the length of input – 3. Let’s see which it is.

Add a print statement. You might find it helpful to distinguish this from the print statement you added earlier so you can more easily see which is the index before updating and which is the index after. For example, you might do something like:

System.out.println("index " + index);
//code
System.out.println("index after updating " + index);

What is the value of index after updating for the last time?

Answer: -1

Q5. Now we can tell that the code isn’t finding the last occurrence of “abc” even though we can see that a third occurrence exists. At what index should the third occurrence of “abc” be found?

Answer: Comment the CORRECT answer

Q6. After the program finds the 2nd occurrence of “abc”, at what index does it start searching for the 3rd occurrence?

Hint: look at the line index = input.indexOf(“abc”,index+4);

Answer: 34

Q7. What are some other examples of input that would also have this problem? Select all that are correct.

a) “kdabcabcjei”
b) “ttabcesoeiabco”
c) “abcbabccabcd”
d) “qwertyabcuioabcp”
e) “abcabcabcabca”

Answer: a), e)

Q8. Imagine your friend wants to get help from Coursera classmates on the discussion forums. Which of the following would be the most helpful way to describe the problem so that others can easily help?

a) My method findAbc() isn’t working.
b) My method findAbc() is giving me the wrong answer.
c) My method findAbc() works on “abcdabc” but is giving me the wrong answer with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”.
d) My method findAbc() works on “abcdabc” but is giving me the wrong answer with input “abcdkfjsksioehgjfhsdjfhksdfhuwabcabcajfieowj”: it prints “bcd, bca” when it should print “bcd, bca, bca”. It also gives the wrong answer with input “kdabcabcjei”, it prints “bca” when it should print “bca, bcj”.

Answer: d)

Q9. What is causing this bug?

a) When the character following “abc” is “a” the program misses the next “abc”
b) When one occurrence of “abc” is followed immediately by another occurrence of “abc”, the while loop breaks
c) When one occurrence of “abc” is followed immediately by another occurrence of “abc”, the method does not find the second “abc” because it starts searching at the “b” rather than at the “a” following the first “abc”
d) The method will never find any occurrences of “abc” after the second one

Answer: c)

Q10. Which change needs to be made to fix the bug?

a) Change the line
String found = input.substring(index+1, index+4);
to
String found = input.substring(index+1, index+3);
b) Change the line
String found = input.substring(index+1, index+4);
to
String found = input.substring(index+1, index+5);
c) Change the line
index = input.indexOf(“abc”,index+4);
to
index = input.indexOf(“abc”,index+3);
d) Change the line
index = input.indexOf(“abc”,index+4);
to
index = input.indexOf(“abc”,index+5);

Answer: c)

Q11. Make the change. Test your method on the input options that would have caused the bug (see question 7). What is the output when you run it with input “abcabcabcabca”?

Enter your answer with the printed strings separated by commas, str1, str2. For example, if the output were “bcq”, “bcd”, and “bcu”, you would write: bcq, bcd, bcu

Answer: Comment the CORRECT answer

>> Next: WEEK 3: Java Programming: Solving Problems with Software Coursera Quiz answers

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error then kindly inform us.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

1 thought on “Java Programming Debugging part | Coursera Week 2 Quiz answers”

Leave a Comment

Your email address will not be published. Required fields are marked *