GATE Computer ScienceHardGATE CS (PYQ pattern)
Assume char is 1 byte, int is 4 bytes with 4-byte alignment, double is 8 bytes with 8-byte alignment, and the compiler inserts only the padding alignment requires. struct S { char c; double d; int i; } arr[5]; struct S *p = &arr[1], *q = &arr[4]; char *r = (char *)q, *s = (char *)&p->i; printf("%td %td %zu", q - p, r - s, sizeof(struct S)); What is printed?
- A. 3 30 13
- B. 3 56 24
- C. 3 44 20
- D. 72 56 24
Correct answer: B. 3 56 24
c sits at offset 0, d must start at 8 (7 bytes padding), i at 16, and the size is rounded up to a multiple of 8: 24. q - p counts elements: 3. q starts at byte 4 * 24 = 96 of arr and p->i at 24 + 16 = 40, so r - s = 56 bytes. Ignoring padding gives size 13; forgetting the tail padding gives 20.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int cnt = 0; int f(int n) { cnt++; if (n <= 1) return 1; if (n == 2) return f(1) + 1; return f(n - 1) + f(n - 2) + f(n - 3); } After f(6)…
- int g(int n) { static int s = 0; s += n; if (n > 0) { g(n - 1); s += n; } return s; } int main() { int x = g(3); int y = g(2); printf("%d…
- void p(int n) { if (n == 0) return; printf("%d ", n); p(n - 1); printf("%d ", n 10); if (n % 2 == 0 && n > 2) p(n - 2); } How many numbers…
- 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…
- 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…