ProgrammingVery hardTCS NQT · Infosys
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[5] = {2, 5, 9, 14, 20}, i
for (i = 1; i < 5; i = i + 1)
a[i] = a[i] - a[i - 1]
end for
for (i = 4; i >= 1; i = i - 1)
a[i] = a[i] + a[i - 1]
end for
Print a[0], a[1], a[2], a[3], a[4]- A. 2 3 6 8 12
- B. 2 5 9 14 20
- C. 2 3 3 5 6
- D. 2 5 9 14 201
Correct answer: B. 2 5 9 14 20
The first loop uses already-updated left neighbours and gives 2, 3, 6, 8, 12. The second loop (running right to left) undoes it: 12 + 8 = 20, 8 + 6 = 14, 6 + 3 = 9, 3 + 2 = 5, restoring the original array.
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, i for (i = 1; i <= 10; i = i + 1) x = ((x << 1) ^ i) & 15 end for…
- What will be the output of the following pseudocode? Integer w[5][5], i, j for (i = 0; i < 5; i = i + 1) for (j = 0; j < 5; j = j + 1) if…
- What will be the output of the following pseudocode? String A = "AXYT", B = "AYZX" Integer L[5][5] = all 0, i, j for (i = 1; i <= 4; i = i…
- What will be the output of the following pseudocode? Integer n = 9, steps = 0 while (n != 1) if (n mod 2 == 0) n = n / 2 else n = 3 n + 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer x, y Set x = 17, y =…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 10) if (i mod 2 == 0) s = s + i end if end for…