Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
easy Solved by 1 students

Array Iteration Loop Boundary

Mark
< Prev
Next >

Assume the String variable str has been properly initialized. The following code segment is intended to count and print the number of strings in the animals array that contain str as a substring.

String[] animals = {"horse", "cow", "goat", "dog", "cat", "mouse"};
int count = 0;
for (int i = 0; i <= animals.length; i++)
{
    if (animals[i].indexOf(str) >= 0)
    {
        count++;
    }
}
System.out.println(count);

Which change is necessary for the code segment to work as intended?

A

The condition in the if statement should be changed to animals[i].equals(str).

B

The Boolean expression in the for loop header should be changed to i < animals.length.

C

The Boolean expression in the for loop header should be changed to i < animals.length - 1.

D

The condition in the if statement should be changed to animals[i].substring(str).

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|