ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
struct A { int a[2]; };
struct B { int *p; };
int v = 5;
struct A x = {{1, 2}}, y;
struct B m = {&v}, n;
y = x; n = m;
y.a[0] = 9; *n.p = 9;
printf("%d %d", x.a[0], v);- A. 9 9
- B. 9 5
- C. 1 5
- D. 1 9
Correct answer: D. 1 9
Struct assignment copies every member, including a whole array, so y.a is a separate copy and x.a[0] stays 1. Copying struct B copies only the pointer, so n.p and m.p both point to v, and v becomes 9 (a shallow copy).
More C Programming questions
- With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)? c union U { int i; double d; char c[10]; };
- What is printed? c int nextid(void) { static int id = 0; return ++id; } int main(void) { int a = nextid(), b = nextid(), c = nextid()…
- What is printed? c int x = 2; switch (x) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); break; default: printf("D"); }
- What is printed? c if (-1 < 1u) printf("yes"); else printf("no");
- What is printed? c int a = 0, b = 0; int x = (++a > 5) && (++b > 0); int y = (++a > 0) || (++b > 0); printf("%d %d %d %d", a, b, x, y);
- What is printed? c printf("%c %d %d", 'A' + 2, 'A' + 2, 'z' - 'a');