ProgrammingModerateTCS NQT · Infosys 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 (i = 1 to 4)
arr[i] = arr[i] + arr[i - 1]
end for
Print arr[4], arr[2]- A. 14 8
- B. 6 5
- C. 14 5
- D. 9 8
Correct answer: A. 14 8
Because arr[i − 1] has already been updated, each slot becomes a running (prefix) sum: 3, 4, 8, 9, 14. So arr[4] = 14 and arr[2] = 8. Using the ORIGINAL left neighbour gives 1 + 5 = 6 and 4 + 1 = 5.
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? (calls counts every call, including the first; mod is the remainder.) Integer calls =…
- What will be the output of the following pseudocode? (& is bitwise AND.) Integer total = 0 for (i = 1 to 15) Integer n = i while (n != 0)…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 0; i < 10; i = i + 1) if (i mod 3 == 0) i = i + 2 end if c…
- What will be the output of the following pseudocode? (&& binds more tightly than ||; ! is NOT.) Integer a = 5, b = 10, c = 15 if (a > b ||…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 6, steps = 0, peak = n while (n != 1) if (n mod…
- What will be the output of the following pseudocode? (reverse(x) returns x with its decimal digits reversed.) Integer n = 87, k = 0 while…