C Programming Questions and Answers – Arrays of Structures – 2

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Arrays of Structures – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[]  =  {1, 2, 3, 4};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d\n", p[1].x);
  16.     }

a) Compile time error
b) 3
c) 2
d) 1
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[] = {1, 2, 3, 4};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d\n", p->x);
  16.     }

a) 1
b) 2
c) 3
d) Compile time error
View Answer

Answer: a
Explanation: None.
advertisement

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[] = {1, 2, 3, 4};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d %d\n", p->x, ++p->x);
  16.     }

a) 1 2
b) 2 2
c) Compile time error
d) Undefined behaviour
View Answer

Answer: b
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     } p[] = {1, 2, 3, 4, 5};
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         foo(p);
  11.     }
  12.     void foo(struct point p[])
  13.     {
  14.         printf("%d %d\n", p->x, p[2].y);
  15.     }

a) 1 0
b) Compile time error
c) 1 somegarbagevalue
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[] = {1, 2, 3, 4, 5};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d %d\n", p->x, p[3].y);
  16.     }

a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

6. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[] = {1, 2, 3, 4, 5};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d %d\n", p->x, (p + 2).y);
  16.     }

a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.

7. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1[] = {1, 2, 3, 4, 5};
  11.         foo(p1);
  12.     }
  13.     void foo(struct point p[])
  14.     {
  15.         printf("%d %d\n", p->x, (p + 2)->y);
  16.     }

a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) undefined behaviour
View Answer

Answer: b
Explanation: None.

8. What will be the output of the following C code on a 64-bit system?

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student s[2];
  9.         printf("%d", sizeof(s));
  10.     }

a) 2
b) 4
c) 16
d) 8
View Answer

Answer: c
Explanation: On a 64-bit system, size of pointer is 8 bytes. Here, we are printing the size of an array of 2 structures, hence, the size will be 2×8 = 16 bytes.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.