?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Counting Characters in an Array of Strings

APCSA-DXLW4E

Consider the following method.

    public static int countChars(String[] words, String sub, int num)
    {
        int count = 0;
        for (int i=0; i<words.length; i++)
        {
           if (words[i].equals(sub)) 
               count = count + num;
        }
        return count;
  }

Which of the following segments of code will print a number that is three times the number that is passed as the third parameter to the countChars method?

A
String[] words2 = {"cat", "dog", "cat", "mouse"};
System.out.println(countChars(words2, "cat", 2));
B
String[] words1 = {"cat", "dog", "cat", "dog", "cat", "mouse"};
System.out.println(countChars(words1, "cat", 2));
C
String[] words3 = {"cat", "dog", "cat", "dog","mouse"};
System.out.println(countChars(words3, "cat", 3));
D
String[] words4 = {"cat", "dog", "cat", "dog", "cat", "cat"};
System.out.println(countChars(words4, "cat", 2));
E
String[] words5 = {"cat", "cat", "cat", "cat", "cat", "cat"};
System.out.println(countChars(words5, "cat", 3));