Loop Analysis With Integer Division
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.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | sgarv2513 | 1 | 2 | 0m 00s | 90 |
| #2 | hitrishabhatia | 1 | 2 | 0m 00s | 90 |
| #3 | y.seong2027 | 1 | 1 | 1m 15s | 25 |
APFIVE