ProgrammingModerateAmazon OA · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1
for (each i from 0 to 3)
res[i] = left
left = left * a[i]
end for
for (each i from 3 down to 0)
res[i] = res[i] * right
right = right * a[i]
end for
Print res[0], res[1], res[2], res[3]- A. 24 12 8 6
- B. 24 24 24 24
- C. 1 2 6 24
- D. 24 12 6 4
Correct answer: A. 24 12 8 6
The first pass stores the product of everything to the left of i (1, 1, 2, 6), the second multiplies in the product to the right (24, 12, 4, 1). So res[i] is the product of all elements except a[i]: 24 12 8 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 a[5] = {2, 3, -2, 4, -1} Integer mx = a[0], mn = a[0], best = a[0], t for…
- 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…
- 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…