ProgrammingVery hardTCS NQT · Infosys · Cognizant GenC
Integers are passed by value and arrays by reference (the reference itself is copied). g(a, x): a[0] = a[0] + x x = x * 2 a = [9, 9] a[1] = x return x A = [1, 2] x = 3 r = g(A, x) print A[0], A[1], x, r What is printed?
- A. 4 6 3 6
- B. 4 2 6 6
- C. 4 2 3 6
- D. 1 2 3 6
Correct answer: C. 4 2 3 6
a[0] += x updates the caller's array to [4, 2]. Then x = x*2 changes only the local copy (6). After a = [9, 9] the parameter points to a new array, so a[1] = x does not touch A. The caller prints 4 2 3 and r = 6.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Consider this Java-style code. int a = 5, b = 0, c = 0; if (a > 3 || ++b > 0) c += 1; if (a < 3 && ++b > 0) c += 10; if ((a > 3) | (++b >…
- Consider the pseudocode (letters a..z are 0..25; mod wraps within the alphabet). r = "" for i = 0 to length(s) - 1: r = r +…
- What will be the output of the following pseudocode? (Print writes its value followed by a space.) Function f(n) If n <= 0 Then Return 0…
- What will be the output of the following pseudocode? (Parameters marked ref are passed by reference; the others by value.) Procedure…
- What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative…