ProgrammingVery hardWipro · TCS NQT pattern
What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array as a.)
What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array as a.)
Integer a[6] = {47, 45, 49, 1, 35, 31}
Integer[] b = a
Function modify(Integer[] x)
for (each i from 0 to 5)
x[i] = x[i] * 3
end for
end Function
modify(b)
for (each i from 0 to 5)
Print a[i]
end for- A. 93, 105, 3, 147, 135, 141
- B. 50, 48, 52, 4, 38, 34
- C. 141, 135, 147, 3, 105, 93
- D. 47, 45, 49, 1, 35, 31
Correct answer: C. 141, 135, 147, 3, 105, 93
Since b = a makes b an alias of the SAME array (pass/assign by reference), modifying b's elements through modify() also changes a. Every element is multiplied by 3, so a prints as [141, 135, 147, 3, 105, 93]. Assuming a stays unchanged (as under pass-by-value) gives the trap [47, 45, 49, 1, 35, 31].
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 bitwise XOR.) Integer x = 98, y = 21 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer x, y Set x = 17, y =…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 10) if (i mod 2 == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer a = 6, b = 4 a = a + b b = a - b a = a - b Print a, b