GATE Computer ScienceHardGATE CS (PYQ pattern)
void g(int **pp, int *q, int k) { *q += k; *pp = q + 1; **pp += *q; q = *pp; *q -= k; } int main() { int a[3] = {1, 2, 3}; int *p = a; g(&p, p, 4); g(&p, p, 1); printf("%d %d %d %td", a[0], a[1], a[2], p - a); } What is printed?
- A. 6 8 3 1
- B. 5 4 6 2
- C. 5 8 11 2
- D. 5 4 7 2
Correct answer: B. 5 4 6 2
Call 1 (q = &a[0], k = 4): a[0] = 5; p becomes &a[1]; a[1] = 2 + 5 = 7; the local q now points to a[1], so a[1] = 7 - 4 = 3. Call 2 receives the updated p (q = &a[1], k = 1): a[1] = 4; p becomes &a[2]; a[2] = 3 + 4 = 7; then a[2] = 7 - 1 = 6. Reassigning the local q does not change the caller, but writing through it does. Output 5 4 6 2.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int a[] = {1, 3, 4, 6, 8, 9, 11, 14}; int i = 0, j = 7, c = 0, it = 0; while (i < j) { it++; if (a[i] + a[j] > 15) { c += j - i; j--; }…
- #define SQ(x) x x #define MAX(a, b) ((a) > (b) ? (a) : (b)) int main() { int i = 6, j = 5, k = 6; int r = MAX(i++, j); int s = MAX(j++…
- int a = -17, b = 5; unsigned u = 5; / 32-bit int and unsigned, C99 or later / printf("%d %d %u", a / b, a % -b, a / u); What is printed?
- int a = 0, b = 0, c = 0; int r = (a++ && b++) || (++c && a++) || (b++ || ++c); printf("%d %d %d %d", r, a, b, c); What is printed?
- unsigned f(unsigned x) { unsigned c = 0; while (x) { x &= x - 1; c++; } return c; } unsigned g(unsigned x) { return x & ~(x - 1); }…
- int i = 2, j = 3, k, m; k = i += j, j -= i, i j; m = (i, j); printf("%d %d %d %d", i, j, k, m); What is printed?