ProgrammingVery hardProduct company OA · code-tracing level

How many times does the body of this loop run?

How many times does the body of this loop run?
// c is an 8-bit unsigned integer: it holds 0 to 255 and arithmetic wraps modulo 256
Integer count = 0
for (c = 250; c <= 255; c = c + 3)
    count = count + 1
end for
Print count
  1. A. 2
  2. B. The loop never ends
  3. C. 3
  4. D. 6

Correct answer: B. The loop never ends

c takes 250, 253, and then 253 + 3 = 256, which wraps to 0. Since every value an 8-bit unsigned variable can hold is ≤ 255, the condition c <= 255 is always true, and the loop cycles forever (0, 3, 6, …, 255, 2, …). '2' is the answer if c could hold 256 and the test failed there.

More Pseudocode and Code Tracing questions

Exams that ask this topic