Loop Equivalence Condition
What condition must be met for countOne and countTwo to produce the same outcome?
Given two integer variables a and b both greater than zero, examine the output of the following methods:
public static int countOne(int a, int b) {
int count = 0;
for (int j = 0; j < a / b; j++) {
count++;
}
return count;
}
public static int countTwo(int a, int b) {
int count = 0;
int index = 0;
while (index < a) {
count++;
index += b;
}
return count;
}
What condition must be met for countOne and countTwo to produce the same outcome?
A
Never, they always produce different results
B
Always, regardless of values of a and b
C
a % b must be zero
D
a % b is not zero but a / b rounds up
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE