ProgrammingHardAmazon OA · Capgemini
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[6] = {1, 2, 3, -2, 2, 1}, c = 0, s
for (each i from 0 to 5)
s = 0
for (each j from i to 5)
s = s + a[j]
if (s == 3)
c = c + 1
end if
end for
end for
Print c- A. 3
- B. 5
- C. 4
- D. 6
Correct answer: B. 5
The loops test every subarray and count those with sum 3: [1, 2], [2, 3, −2], [3], [3, −2, 2] and [2, 1], so c = 5. Because of the −2, a running sum can come back to 3 after passing it, so stopping at the first hit misses some.
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 m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body) Integer c =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "110100" Integer v = 0, p = 1 for (each i…
- What will be the output of the following pseudocode? Boolean mark[2..30] = all false Integer ops = 0, j for (each p from 2 to 30) if (p p…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)…
- What will be the output of the following pseudocode? Stack s1 = empty, s2 = empty Procedure enqueue(Integer x) push(s1, x) End Procedure…