ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 5, t = 0
for (i = 1 to n)
if (i mod 2 == 1)
t = t + i * i
else
t = t - i
end if
end for
Print t- A. 29
- B. 35
- C. 41
- D. 23
Correct answer: A. 29
Odd squares 1 + 9 + 25 = 35, minus the even numbers 2 + 4 = 6 → 29. Forgetting the subtraction gives 35.
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 a = 1, b = 1, t for (k = 1 to 5) t = a + b a = b b = t if (b mod 2 == 0)…
- What will be the output of the following pseudocode? Integer Function f(Integer n, Integer acc) if (n == 0) return acc return f(n - 1…
- What will be the output of the following pseudocode? Integer x = 100, c = 0 while (x > 1) if (x mod 2 == 0) x = x / 2 else x = 3 x + 1 c =…
- What will be the output of the following pseudocode? Integer a[] = {1, 3, 5, 7, 9, 11, 13} Integer lo = 0, hi = 6, t = 11, steps = 0, mid…
- What will be the output of the following pseudocode? Integer x = 15, y = 4, r = 0 while (x >= y) x = x - y r = r + 1 end while Print r, x
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…