ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (product of array except self, without using division)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (product of array except self, without using division)? (Arrays are indexed from 0.)
Integer arr[4] = {1, 2, 3, 4}
Integer n = 4
Integer res[4] = {1, 1, 1, 1}
Integer left = 1
for (each i from 0 to n - 1)
res[i] = left
left = left * arr[i]
end for
Integer right = 1
for (each i from n - 1 down to 0)
res[i] = res[i] * right
right = right * arr[i]
end for
Print res- A. [24, 12, 8, 6]
- B. [6, 8, 12, 24]
- C. [1, 2, 3, 4]
- D. [24, 24, 24, 24]
Correct answer: A. [24, 12, 8, 6]
res[i] ends up as the product of all elements except arr[i]: res[0]=2*3*4=24, res[1]=1*3*4=12, res[2]=1*2*4=8, res[3]=1*2*3=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 (how many output rows a zigzag string conversion uses)? Function numRowsUsed(String s…
- What will be the output of the following pseudocode (counting islands in a grid using flood-fill DFS)? (Grids are indexed from 0.) Integer…
- What will be the output of the following pseudocode (summing positive and negative matrix entries separately)? Integer mat[3][3] = {{1…
- What will be the output of the following pseudocode (counting distinct pairs whose difference is k)? (Arrays are indexed from 0.) Integer…
- What will be the output of the following pseudocode (2x2 matrix multiplication)? Integer A[2][2] = {{1, 2}, {3, 4}} Integer B[2][2] = {{5…
- What will be the output of the following pseudocode (binary string to decimal)? String bits = "110101" Integer val = 0 for (each b in…