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 % b is equal to zero.
B
When a % b is equal to one.
C
When a and b are both even.
D
When a and b are both odd.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | hitrishabhatia | 1 | 2 | 0m 00s | 90 |
| #2 | sgarv2513 | 1 | 2 | 0m 00s | 90 |
| #3 | y.seong2027 | 1 | 1 | 1m 15s | 25 |
| #4 | bommasam000 | 1 | 2 | 24m 11s | -1,361 |
| #5 | Pelisx | 1 | 4 | 57m 38s | -3,388 |
| #6 | midnightasdark | 1 | 4 | 4h 17m | -15,360 |
Items per page:
10
1 – 6 of 6
APFIVE