ProgrammingHardTCS Digital · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0, i
for (i = 0; i < 10; i = i + 1)
if (i mod 3 == 0)
i = i + 2
end if
c = c + 1
end for
Print c, i- A. 4 11
- B. 10 10
- C. 5 12
- D. 4 12
Correct answer: D. 4 12
Changing i inside the body skips values: i = 0 → 2 (c = 1) → 3 → 5 (c = 2) → 6 → 8 (c = 3) → 9 → 11 (c = 4) → the update makes i = 12, and 12 < 10 fails. So c = 4 and i = 12. '4 11' forgets the final i = i + 1 that runs before the failing 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? (&& binds more tightly than ||; ! is NOT.) Integer a = 5, b = 10, c = 15 if (a > b ||…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 6, steps = 0, peak = n while (n != 1) if (n mod…
- What will be the output of the following pseudocode? (reverse(x) returns x with its decimal digits reversed.) Integer n = 87, k = 0 while…
- What will be the output of the following pseudocode? Procedure fun(Integer n) if (n <= 0) return end if fun(n - 2) Print n fun(n - 1) End…
- What will be the output of the following pseudocode? (/ is integer division.) Integer a = 1, b = 100, steps = 0 while (a < b) a = a 2 b =…
- What will be the output of the following pseudocode? (calls counts every call, including the first; memo starts with every entry equal to…