ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? (Arrays are passed by reference and indexed from 0.)
What will be the output of the following pseudocode? (Arrays are passed by reference and indexed from 0.)
Procedure p(Integer arr[], Integer n)
if (n == 0)
return
end if
arr[n - 1] = arr[n - 1] + arr[n mod 3]
p(arr, n - 1)
End Procedure
Integer a[4] = {1, 2, 3, 4}
p(a, 4)
Print a[0], a[1], a[2], a[3]- A. 2 4 4 6
- B. 7 6 4 6
- C. 7 4 4 6
- D. 3 6 4 6
Correct answer: B. 7 6 4 6
The recursion goes from the last index down, and each update reads the CURRENT array. n = 4: a[3] += a[1] → 6. n = 3: a[2] += a[0] → 4. n = 2: a[1] += a[2] (now 4) → 6. n = 1: a[0] += a[1] (now 6) → 7. Result 7 6 4 6. Using the original values everywhere gives 2 4 4 6-style answers; arrays passed by reference see every earlier write.
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? (/ is integer division.) Integer c = 0, i = 1, j while (i < 1000) j = i while (j > 0)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0 for (n = 1 to 200) Integer s = 0, t = n while…
- What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop…
- What will be the output of the following pseudocode? (&& stops as soon as the left side is false) Integer c = 0, k = 0 Function f(Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Procedure show(Integer n) if…
- What will be the output of the following pseudocode? (| is bitwise OR) Integer x = 0, c = 0 while (x < 1000) x = x | (x + 1) c = c + 1 end…