ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode? (Arrays are indexed from 0)
What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer a[7] = {-7, 1, 5, 2, -4, 3, 0}
Integer total = 0, left = 0, ans = -1
for (each i from 0 to 6)
total = total + a[i]
end for
for (each i from 0 to 6)
if (left == total - left - a[i])
ans = i
break
end if
left = left + a[i]
end for
Print ans- A. 6
- B. -1
- C. 2
- D. 3
Correct answer: D. 3
total = 0. The first i where the sum on the left equals the sum on the right: at i = 3, left = −7 + 1 + 5 = −1 and right = −4 + 3 + 0 = −1. ans = 3 (index 6 also works, but the loop stops at the first).
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[5] = {1, 5, 3, 4, 2}, k = 2, c = 0 for (each i from 0 to 4) for (each j…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to i i) if (j mod…
- What is the extra space (memory) used by the following recursive pseudocode as a function of n, counting the call stack? Function…
- What will be the output of the following pseudocode? Integer pas[0..6][0..6] = all 0 for (each i from 0 to 6) pas[i][0] = 1 for (each j…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer a[8] = {4, 9, 1, 7, 3…
- What will be the output of the following pseudocode? Integer c = 0 for (each a from 1 to 20) for (each b from a to 20) for (each h from b…