| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice Test
Share
medium Solved by 3 students
Loop Analysis With Integer Division
< Prev
Next >

Consider the following two methods.

/** Precondition: a > 0 and b > 0 */
public static int methodOne(int a, int b)
{
    int loopCount = 0;
    for (int i = 0; i < a / b; i++)
    {
        loopCount++;
    }
    return loopCount;
}

/** Precondition: a > 0 and b > 0 */
public static int methodTwo(int a, int b)
{
    int loopCount = 0;
    int i = 0;
    while (i < a)
    {
        loopCount++;
        i += b;
    }
    return loopCount;
}

Which of the following best describes the conditions under which methodOne and methodTwo return the same value?

A

When a and b are both odd.

B

When a % b is equal to one.

C

When a and b are both even.

D

When a % b is equal to zero.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1sgarv251312 0m 00s 90
#2hitrishabhatia12 0m 00s 90
#3y.seong202711 1m 15s 25
APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy