ProgrammingHardInfosys · Capgemini pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 5, s = 0
for (i = 1 to n)
for (j = 1 to i)
if ((i + j) mod 2 == 0)
s = s + i * j
end if
end for
end for
Print s- A. 55
- B. 225
- C. 70
- D. 86
Correct answer: D. 86
Only pairs with i + j even (same parity) count: i = 1: 1; i = 2: 4; i = 3: 3 + 9 = 12; i = 4: 8 + 16 = 24; i = 5: 5 + 15 + 25 = 45. Total 1 + 4 + 12 + 24 + 45 = 86.
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 v = 1 for (k = 0 to 4) v = v 2 + k end for Print v
- What will be the output of the following pseudocode? Integer calls = 0 Integer Function f(Integer n) calls = calls + 1 if (n <= 2) return…
- What will be the output of the following pseudocode? Integer a = 5, b, c, d b = a++ // post-increment c = ++a // pre-increment d = a + b +…
- What will be the output of the following pseudocode? Integer a[5] = {2, 4, 6, 8, 10} for (i = 1 to 4) a[i] = a[i] + a[i - 1] end for Print a
- 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…
- 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…