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- A. 2
- B. The loop never ends
- C. 3
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (/ is integer division.) Integer a = 7, b = 3 a = a b - a / b Print a
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 20) if (i mod 3 == 0 or i mod 5 == 0) c = c + 1 end if…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 2405, c = 0, s = 0 while (n > 0) s = s + n mod…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 4) for (j = 1 to 4) if (j > i) break end if c = c + j end…
- What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift; precedence as in C: << binds…
- What will be the output of the following pseudocode? Function f(Integer n) if (n <= 1) return 1 end if return f(n - 1) + 2 f(n - 2) End…