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);
  1. A. 9 9
  2. B. 9 5
  3. C. 1 5
  4. 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

Exams that ask this topic