ProgrammingModerateTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[] = {10, 20, 30, 40}
Integer i = 0, s = 0
while (i < 4)
s = s + a[i]
i = i + 2
if (i == 4) i = 1
end while
Print s- A. 40
- B. 60
- C. 100
- D. 80
Correct answer: C. 100
Indices visited: 0, 2, then i becomes 4 and is reset to 1, then 3, then 5 ends the loop. All four elements are added: 100. Without the reset only a[0] + a[2] = 40.
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? (memo starts with every entry equal to −1; calls counts every call, including the…
- What will be the output of the following pseudocode? (calls counts every call, including the first.) Integer calls = 0 Function g(Integer…
- What will be the output of the following pseudocode? (& is bitwise AND and >> is a right shift.) Integer c = 0 for (each x from 1 to 200)…
- What will be the output of the following pseudocode? (Integers are in two's complement, so −i flips the bits of i and adds 1; & is bitwise…
- What will be the output of the following pseudocode? (& is bitwise AND.) Integer c = 0, x for (each i from 0 to 63) x = i while (x > 0) x…
- What will be the output of the following pseudocode? Integer n = 30, c = 0, j for (each i from 1 to n) j = i while (j <= n) c = c + 1 j =…