| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
medium Solved by 3 students
Array Traversal Loop Bounds
< Prev
Next >

The countPeaks method below is intended to return the number of local minimums in an integer array. A local minimum is an element that is less than both of its adjacent elements. The first and last elements of the array are not considered local minimums. For example, for the array {3, 1, 7, 4, 2, 12, 3, 5}, the method should return 3.

public static int countPeaks(int[] data)
{
    int numPeaks = 0;
    for ( /* missing loop header */ ) {
        if (data[p - 1] > data[p] && data[p] < data[p + 1])
        {
            numPeaks++;
        }
    }
    return numPeaks;
}

Which of the following can replace /* missing loop header */ so that the method works as intended?

A

int p = 0; p < data.length - 1; p++

B

int p = 1; p < data.length - 1; p++

C

int p = 0; p < data.length; p++

D

int p = data.length - 1; p > 0; p–

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.

AI Tutor

How can I help?

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