?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Debugging Code: Counting Characters in String

APCSA-EDKTB4

Consider the following code.

// Return the count of the number of times c is found in phrase
// Precondition: c is a single character
public static int countChar(String phrase, String c)
{
     int count=0;
     for (int i=0; i<phrase.length(); i++);
     {
        if (phrase.substring(i, i+1).equals(c))
            count++;
     }
     return count;
}

Which of the following BEST describe why this code will not compile successfully?

A

The method will throw an ArrayIndexOutOfBoundsException.

B

There is a syntax error: i cannot be resolved as a variable in the if statement.

C

The returned count is always too high.

D

The substring command is causing an OutOfBoundsException.

E

Nothing is wrong with the code. It should function as expected.