ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0, i
for (i = 1; i <= 50; i = i + 1)
i = i * 2
c = c + 1
end for
Print c, i- A. 50 51
- B. 6 127
- C. 5 63
- D. 5 62
Correct answer: C. 5 63
The body doubles i and then the loop adds 1, so i runs 1 → 2 → 3, 3 → 6 → 7, 7 → 14 → 15, 15 → 30 → 31, 31 → 62 → 63. After the fifth pass i = 63 > 50, the test fails, and the loop ends with c = 5 and i = 63. '5 62' forgets the final increment, which happens before the test.
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? (Arrays are indexed from 0.) Integer a[8] = {1, 2, 3, 4, 6, 7, 8, 9} Integer i = 0, j…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; a 'from a down to b' loop does nothing when a < b.)…
- What will be the output of the following pseudocode? // 32-bit signed integers; overflow wraps around (two's complement) Integer f = 1 for…
- What will be the output of the following pseudocode? Integer a = 100, b = 7, steps = 0 while (a != b) if (a > b) a = a - b else b = b - a…
- What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.) Integer calls…
- What will be the output of the following pseudocode? (^ is bitwise XOR and >> is a right shift.) Integer g = 59, n = 0 while (g > 0) n = n…