ProgrammingVery hardAmazon OA · Infosys SP
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[8] = {1, 1, 2, 2, 2, 3, 5, 5}
Integer k = 1
for (each i from 1 to 7)
if (a[i] != a[k - 1])
a[k] = a[i]
k = k + 1
end if
end for
Print k, a[k - 1], a[k]- A. 4 5 5
- B. 4 3 5
- C. 4 5 2
- D. 8 5 5
Correct answer: C. 4 5 2
The loop compacts the distinct values to the front: {1, 2, 3, 5, …}, so k = 4 and a[3] = 5. The rest of the array is untouched old data; a[4] still holds the original 2. Output 4 5 2.
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? (strings are indexed from 0) String x = "AGGTAB", y = "GXTXAYB" Integer L[0..6][0..7]…
- What will be the output of the following pseudocode? Integer wt[3] = {2, 3, 4}, val[3] = {3, 5, 6} Integer best[0..6] = all 0 for (each i…
- What will be the output of the following pseudocode? Integer lcm = 18, steps = 0 Integer a = 12, b = 18 while (lcm mod a != 0 OR lcm mod b…
- What will be the output of the following pseudocode? Integer total = 0 Procedure p(Integer n) if (n <= 0) return end if total = total + n…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 8) for (each j from i + 1 to 9) for (each k from…
- What will be the output of the following pseudocode? Function f(Integer a, Integer b) return a b - (a + b) End Function Print f(f(2, 3)…