ProgrammingHardAmazon OA · Capgemini
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[6] = {3, 2, 2, 3, 4, 3}, k = 0, val = 3
for (each i from 0 to 5)
if (a[i] != val)
a[k] = a[i]
k = k + 1
end if
end for
Print k, a[0], a[2], a[4]- A. 3 2 4 3
- B. 3 2 4 4
- C. 3 2 2 4
- D. 2 2 4 4
Correct answer: B. 3 2 4 4
The kept values 2, 2, 4 are copied to the front, so k = 3 and a = {2, 2, 4, 3, 4, 3} (the tail keeps its old values). Output 3 2 4 4.
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? (All variables are integers, so / is integer division) Integer n = 7, c = 1 for (each…
- What will be the output of the following pseudocode? Integer count = 0 String last = "" Procedure gen(String s, Integer n) if (length(s)…
- What will be the output of the following pseudocode? Integer p[4] = {2, 0, 3, 1} // coefficients of 2x³ + 0x² + 3x + 1 Integer x = 3, m =…
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i i <= n; i = i + 1) for (j = 1; j <= n; j = j 2)…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…