C Programming Questions and Answers – Pointer to Structures – 2

«
»

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

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

advertisement

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

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", s->c);
  12.     }

a) hello
b) Run time error
c) Nothing
d) Depends on compiler
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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

Take C Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement
  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student *s;
  9.         s->c = "hello";
  10.         printf("%s", s->c);
  11.     }

a) hello
b) Segmentation fault
c) Run time error
d) Nothing
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", m.c);
  12.     }

a) Run time error
b) Nothing
c) hello
d) Varies
View Answer

Answer: c
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         (*s).c = "hello";
  11.         printf("%s", m.c);
  12.     }

a) Run time error
b) Nothing
c) Varies
d) hello
View Answer

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student n;
  9.         struct student *s = &n;
  10.         (*s).c = "hello";
  11.         printf("%p\n%p\n", s, &n);
  12.     }

a) Different address
b) Run time error
c) Nothing
d) Same address
View Answer

Answer: d
Explanation: None.

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

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

a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
View Answer

Answer: b
Explanation: None.

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

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

a) Compile time error
b) Undefined behaviour
c) Segmentation fault/code crash
d) 1
View Answer

Answer: b
Explanation: None.

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

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

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

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / sizeof(struct p));
  12.         printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
  13.     }

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

Answer: d
Explanation: None.

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.

advertisement
advertisement

Leave a Comment

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 & technical discussions at Telegram SanfoundryClasses.