Java OCA OCP Practice Question 1142

Question

Which of the following iterates a different number of times than the others?

  • A. for (int k=0; k < 5; k++) {}
  • B. for (int k=1; k <= 5; k++) {}
  • C. int k=0; do { } while(k++ < 5)
  • D. int k=0; while (k++ < 5) {}


C.

Note

Option A goes through five indexes on the iterations: 0, 1, 2, 3 and 4.

Option B also goes through five indexes: 1, 2, 3, 4 and 5.

Option D goes through five iterations as well, from 0 to 4.

However, Option C goes through six iterations since the loop condition is at the end of the loop.

Therefore it is not like the others, and Option C is the answer.




PreviousNext

Related