| 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
< Prev
Next >

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

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy