ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
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 (s > 20) break
end while
Print i, s- A. 6 17
- B. 8 24
- C. 7 24
- D. 7 28
Correct answer: C. 7 24
s grows 1, 3, 6, (4 skipped), 11, 17, 24; at i = 7 the sum exceeds 20 and the loop breaks. Including 4 would give 1 + 2 + 3 + 4 + 5 + 6 = 21 at i = 6.
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 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…
- What will be the output of the following pseudocode? Integer a = 13, b = 10 // 1101 and 1010 Print a & b, a | b, a ^ b, ~a // 32-bit two's…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 0) return 0 return f(n / 2) 10 + (n mod 2) End…
- What will be the output of the following pseudocode? Stack st = empty for (each x in {3, 1, 4, 1, 5, 9, 2, 6}) while (st not empty AND…