ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer val[6] = {5, 8, 2, 9, 4, 7}
Integer nxt[6] = {3, 5, 0, 1, -1, 4}
Integer p = 2, s = 0, k = 0
While p != -1
If k mod 2 = 0 Then
s = s + val[p]
Else
s = s - val[p]
End If
p = nxt[p]
k = k + 1
End While
Print s, k- A. -1 6
- B. 1 6
- C. 5 6
- D. 1 5
Correct answer: B. 1 6
The chain from index 2 is 2 -> 0 -> 3 -> 1 -> 5 -> 4 -> end (6 nodes). With alternating signs: +2 - 5 + 9 - 8 + 7 - 4 = 1, and k counts 6 visited nodes.
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 a[51] = all zeros (indices 0 to 50) For i = 1 to 50 For j = i to 50 step i…
- What will be the output of the following pseudocode? (ord('A') = 65; the letters are uppercase and the mod keeps every shift inside the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {5, 1, 4, 2, 8, 0, 3} Integer swaps = 0…
- What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {1, 2, 4, 5, 7, 9, 10} Integer i = 0, j = 6, c…
- What will be the output of the following pseudocode? (Division is integer division; arrays are 0-indexed.) Integer a[9] = {2, 3, 3, 3, 5…