ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer total = 0, n = 5
for (each i from 1 to n)
for (each j from i to n)
total = total + 1
end for
end for
Print total- A. 25
- B. 10
- C. 20
- D. 15
Correct answer: D. 15
For each i, j runs from i to n, giving n-i+1 iterations: i=1 gives 5, i=2 gives 4, i=3 gives 3, i=4 gives 2, i=5 gives 1. Total 5+4+3+2+1=15.
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? Function fact(Integer n) if (n <= 1) return 1 end if return n fact(n - 1) End…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function rec(Integer n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function power(Integer a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 17, i = 2 Boolean…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer n = 8…
- What will be the output of the following pseudocode? Function countdownSum(Integer n) if (n == 0) return 0 end if return n +…