ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0, n = 5
for (each i from 1 to n)
for (each j from 1 to 2 * i - 1)
c = c + 1
end for
end for
Print c- A. 15
- B. 20
- C. 25
- D. 30
Correct answer: C. 25
For each i, the inner loop runs 2i-1 times: 1,3,5,7,9 for i=1..5. Summing these odd numbers gives 1+3+5+7+9 = 25 (the sum of the first n odd numbers equals n^2 = 25).
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? (Arrays are indexed from 0.) Integer arr[6] = {4, 8, 15, 16, 23, 42} Integer target =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function countDigits(Integer…
- What will be the output of the following pseudocode (Pascal's identity for combinations)? Function C(Integer n, Integer r) if (r == 0 or r…
- What will be the output of the following pseudocode (Armstrong number check)? Function isArmstrong(Integer n) String s = toString(n)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (j == 2) break end…
- What will be the output of the following pseudocode (Kadane's maximum subarray sum)? (Arrays are indexed from 0.) Integer arr[9] = {-2, 1…