GATE Computer ScienceHardGATE CS (PYQ pattern)
Assume sizeof(int) = 4 and every pointer is 8 bytes. int f(int a[][3], char s[]) { return sizeof(a) + sizeof(*a) + sizeof(s); } int main() { int m[4][3]; char t[] = "gate"; int i = 5; size_t k = sizeof(m[i++]) + sizeof "cs\0e"; printf("%d %zu %d", f(m, t), k, i); } What is printed?
- A. 28 17 6
- B. 68 17 5
- C. 28 15 5
- D. 28 17 5
Correct answer: D. 28 17 5
Inside f, a is really int (*)[3] (8 bytes) and *a is a row of 3 ints (12); s is a char * (8): 28. The operand of sizeof is not evaluated (it is not a VLA), so i++ never happens and m[5] is never touched; m[i++] has type int[3], 12 bytes. The literal "cs\0e" occupies 4 characters plus its own terminator = 5 bytes, so k = 17 and i stays 5.
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[] = {5, 6, 7, 8}; int p = a; p++ += 2; (p)++; ++p += 10; ++p; What are a[0], a[1], a[2], a[3] and p - a after these statements?
- 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)…
- 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?