Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
medium Solved by 2 students

Analyzing Array Traversal Code

< Prev
Next >

An integer array nums has been declared and initialized with one or more values. Which of the following code segments correctly counts the number of negative values in nums and stores the result in the counter variable?

I.

int counter = 0;
int i = -1;
while (i <= nums.length - 2)
{
    i++;
    if (nums[i] < 0)
    {
        counter++;
    }
}

II.

int counter = 0;
for (int i = 1; i < nums.length; i++)
{
    if (nums[i] < 0)
    {
        counter++;
    }
}

III.

int counter = 0;
for (int i : nums)
{
    if (nums[i] < 0)
    {
        counter++;
    }
}
A

I, II, and III

B

I and III only

C

II only

D

I only

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1kaisuki11 0m 00s 100
#2deltasmpserver01 0m 00s -10
#3hmbin99901 1m 03s -73
#4y.seong202713 10m 28s -548
Items per page:
10
1 – 4 of 4
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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