ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
/* x86-64 LP64: natural alignment (double 8, int 4, short 2) */
struct X { char a[5]; double d; char e; };
struct Y { char a[5]; int i; short s; };
struct Z { short s; char a[3]; };
printf("%zu %zu %zu", sizeof(struct X), sizeof(struct Y), sizeof(struct Z));- A. 14 11 5
- B. 24 16 6
- C. 24 12 6
- D. 16 16 6
Correct answer: B. 24 16 6
A char array has alignment 1, so only the scalar members force padding. X: a at 0–4, d at 8, e at 16, size rounded up to a multiple of 8 → 24. Y: a at 0–4, i at 8, s at 12, size rounded to a multiple of 4 → 16. Z: s at 0, a at 2–4, rounded to a multiple of 2 → 6. 14/11/5 are the packed sums.
More C Programming questions
- What is printed? c void p(const char s) { if (!s) return; putchar(s); p(s + 2); putchar(s); } int main(void) { p("abcdef"); }
- What is printed? c / LP64: 4-byte int, 8-byte pointers / int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = a; printf("%d %d %zu %zu"…
- What is printed? c int i, c = 0; for (i = 0; i < 6; i++) if (i % 2 == 0 || ++c > 1) c += 10; printf("%d %d", i, c);
- What is printed? c / 16-bit unsigned short, 32-bit int and unsigned / unsigned short a = 3, b = 5; printf("%d %u %d", a - b, 3u - 5u…
- What is printed? c #define ABS(x) x < 0 ? -x : x printf("%d %d", ABS(2 - 5), ABS(-4) 2);
- What is printed? c const char s = "mississippi"; printf("%td %td %td", strchr(s, 's') - s, strrchr(s, 's') - s, strstr(s, "ssip") - s);