Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
hard

Loop Equivalence Condition

Mark
< Prev
Next >

Consider the following two Java methods, where integer arguments a and b are both greater than zero.

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;
}

Under which condition will the countOne and countTwo methods return the same value?

A

Always, regardless of values of a and b

B

a % b is not zero but a / b rounds up

C

a % b must be zero

D

Never, they always produce different results

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

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

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