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] = {1, 2, 3, 4, 5}
Integer s = 0
for (each i from 1 to 5)
s = s + arr[i - 1]
end for
Print s- A. 10
- B. 15
- C. 20
- D. 12
Correct answer: B. 15
The loop runs i=1..5, and arr[i-1] correctly accesses arr[0] through arr[4] (compensating for 1-based counting against a 0-indexed array), summing the whole array: 1+2+3+4+5=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? (Matrices are indexed from 0.) Integer mat[3][2] = {{1, 2}, {3, 4}, {5, 6}} Integer…
- What will be the output of the following pseudocode (merging two sorted arrays)? (Arrays are indexed from 0.) Integer a[4] = {1, 3, 5, 7}…
- What will be the output of the following pseudocode? Integer c = 0, n = 5 for (each i from 1 to n) for (each j from 1 to 2 i - 1) c = c +…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {4, 8, 15, 16, 23, 42} Integer target =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function countDigits(Integer…
- What will be the output of the following pseudocode (Pascal's identity for combinations)? Function C(Integer n, Integer r) if (r == 0 or r…