GATE Computer ScienceVery hardGATE CS (PYQ pattern)
void f(int *x, int *y, int z) { *x = *y + z; *y = *x - z; z = *x; } int main() { int a = 1, b = 2, c = 3; f(&a, &b, c); f(&b, &a, c); f(&a, &a, c); printf("%d %d %d", a, b, c); } What is printed?
- A. 5 8 3
- B. 5 8 8
- C. 8 8 3
- D. 5 5 3
Correct answer: A. 5 8 3
f(&a,&b,3): a = 2+3 = 5, b = 5-3 = 2. f(&b,&a,3): b = 5+3 = 8, a = 8-3 = 5. f(&a,&a,3) aliases both pointers: a = 5+3 = 8, then a = 8-3 = 5. c stays 3, being passed by value.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int h(int n) { return n == 0 ? 0 : 2 h(n / 10) + n % 10; } What is h(2593)?
- int A(int m, int n) { if (m == 0) return n + 1; if (n == 0) return A(m - 1, 1); return A(m - 1, A(m, n - 1)); } How many calls to A…
- int f(int n); int g(int n) { return n <= 0 ? 1 : f(n - 1) + g(n - 2); } int f(int n) { return n <= 0 ? 0 : g(n - 1) + n; } What does f(6)…
- void r(char s) { if (s[0] == 0 || s[1] == 0) { printf("%s", s); return; } printf("%c%c", s[1], s[0]); r(s + 2); printf("%c", s[0]); } What…
- int t(int n) { return n == 0 ? 0 : n % 3 + 10 t(n / 3); } What is t(97) + t(t(7))?
- int g(int a, int b) { return b == 0 ? a : g(b, a % b); } How many calls to g (including the first) does g(610, 987) make, and how many…