ProgrammingModerateInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer x = 1
for (i = 1 to 4)
if (i mod 2 == 1)
x = x * i
else
x = x + i
end if
end for
Print x- A. 13
- B. 24
- C. 10
- D. 15
Correct answer: A. 13
i = 1: x = 1·1 = 1; i = 2: 1 + 2 = 3; i = 3: 3·3 = 9; i = 4: 9 + 4 = 13. Mixing up which branch handles odd i would give 1 + 1 = 2, 4, 7, 28.
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? Integer n = 10, s = 0 for (i = 1 to n) if (n mod i == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer Function f(Integer x, Integer y) if (x > y) return f(x - y, y) if (x < y)…
- What will be the output of the following pseudocode? Integer s = 0, i = 0 while (true) i = i + 1 if (i mod 4 == 0) continue s = s + i if…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer d1 = 0, d2 = 0, t = 0 for…
- What will be the output of the following pseudocode? Integer c = 0 Procedure rec(Integer k) c = c + 1 if (k <= 1) return rec(k / 2) rec(k…
- What will be the output of the following pseudocode? // 32-bit signed integers with two's-complement wrap-around Integer x = 2147483647…