ProgrammingHardTCS NQT · Accenture pattern
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 arr[5] = {3, 1, 4, 1, 5}
for (each i from 1 to 4)
arr[i] = arr[i] + arr[i - 1]
end for
Print arr[4] - arr[1]- A. 9
- B. 11
- C. 4
- D. 10
Correct answer: D. 10
The loop turns the array into running (prefix) sums {3, 4, 8, 9, 14}. arr[4] - arr[1] = 14 - 4 = 10, which is the sum of the original elements at indices 2 to 4 (4 + 1 + 5). Using the original values gives 5 - 1 = 4.
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 x = 0 for (each i from 1 to 100) if (i mod 4 == 0) continue end if if (i >…
- What will be the output of the following pseudocode? Integer a = 10, b = 3, c = 0 while (a >= b) a = a - b c = c + 1 end while Print c, a
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 5) for (each j from 1 to 5) if (j > i) break end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 2048, c = 0, d…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n…