ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the caller's array; assigning a new array to the parameter changes only what the local name refers to.)
What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the caller's array; assigning a new array to the parameter changes only what the local name refers to.)
Procedure change(Integer x, Integer arr[])
x = x + 10
arr[0] = arr[0] + x
arr = new array {100, 200}
arr[1] = 0
End Procedure
Integer v = 5
Integer a[2] = {1, 2}
change(v, a)
Print v, a[0], a[1]- A. 5 16 2
- B. 15 16 0
- C. 5 16 0
- D. 5 100 0
Correct answer: A. 5 16 2
x is a copy, so v stays 5; inside, x becomes 15. arr refers to the caller's array, so arr[0] = 1 + 15 = 16 changes a[0]. Then arr is pointed at a brand-new array; arr[1] = 0 changes that new array, not a. So a = {16, 2}. '5 16 0' assumes the reassignment still affects the caller's array; '15 16 0' also treats x as passed by reference.
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? (&& binds more tightly than ||, and both stop evaluating as soon as the result is…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 1; i <= 50; i = i + 1) i = i 2 c = c + 1 end for Print c, i
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[8] = {1, 2, 3, 4, 6, 7, 8, 9} Integer i = 0, j…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; a 'from a down to b' loop does nothing when a < b.)…
- What will be the output of the following pseudocode? // 32-bit signed integers; overflow wraps around (two's complement) Integer f = 1 for…
- What will be the output of the following pseudocode? Integer a = 100, b = 7, steps = 0 while (a != b) if (a > b) a = a - b else b = b - a…