ProgrammingHardCapgemini · Infosys pattern
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 arr[4] = {2, 3, 1, 0}
for (each i from 0 to 3)
arr[i] = arr[arr[i]]
end for
Print arr[0], arr[1], arr[2], arr[3]- A. 1 0 1 0
- B. 1 0 3 2
- C. 0 1 1 0
- D. 1 0 0 1
Correct answer: D. 1 0 0 1
The array changes in place, so later steps read values already overwritten. i = 0: arr[0] = arr[2] = 1. i = 1: arr[1] = arr[3] = 0. i = 2: arr[2] = arr[1] = 0 (already changed). i = 3: arr[3] = arr[0] = 1. Using the original array gives the trap 1 0 3 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? Function f(Integer n) if (n > 100) return n - 10 end if return f(f(n + 11)) End…
- What will be the output of the following pseudocode? Function A(Integer m, Integer n) if (m == 0) return n + 1 end if if (n == 0) return…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 4) for (each j from 1 to 4) if ((i j) mod 3 == 0)…
- What will be the output? (A do-while loop runs its body once before checking the condition; / is integer division.) Integer x = 0, c = 0…
- What will be the output of the following pseudocode? Integer p = 1 for (each i from 1 to 10) p = (p 3) mod 7 end for Print p
- The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.) Integer low = 0, high = 8…