C Programming Questions and Answers – Bit-fields – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Bit-fields – 2”.

Pre-requisite for C Bit-fields MCQ set: Video Tutorial on C Bit-fields.

advertisement

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         char x : 2;
  5.         int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 2;
  11.         p.y = 1;
  12.         p.x = p.x & p.y;
  13.         printf("%d\n", p.x);
  14.     }

a) 0
b) Compile time error
c) Undefined behaviour
d) Depends on the standard
View Answer

Answer: a
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     union u
  3.     {
  4.         struct p
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         };
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u;
  14.         u.p.x = 2;
  15.         printf("%d\n", u.p.x);
  16.     }

a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 2
View Answer

Answer: a
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u;
  14.         u.p.x = 2;
  15.         printf("%d\n", u.p.x);
  16.     }

a) Compile time error
b) 2
c) Undefined behaviour
d) Depends on the standard
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u.p.x = 2;
  14.         printf("%d\n", u.p.x);
  15.     }

a) Compile time error
b) 2
c) Depends on the compiler
d) Depends on the standard
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u = {2};
  14.         printf("%d\n", u.p.x);
  15.     }

a) Compile time error
b) 2
c) Depends on the standard
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

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

  1.     #include <stdio.h>
  2.     union u
  3.     {
  4.         struct
  5.         {
  6.             unsigned char x : 2;
  7.             unsigned int y : 2;
  8.         }p;
  9.         int x;
  10.     };
  11.     int main()
  12.     {
  13.         union u u.p = {2};
  14.         printf("%d\n", u.p.x);
  15.     }

a) Compile time error
b) 2
c) Undefined behaviour
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         unsigned int x : 2;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 3;
  11.         p.y = 1;
  12.         printf("%d\n", sizeof(p));
  13.     }

a) Compile time error
b) Depends on the compiler
c) 2
d) 4
View Answer

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         unsigned int x : 2;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 3;
  11.         p.y = 4;
  12.         printf("%d\n", p.y);
  13.     }

a) 0
b) 4
c) Depends on the compiler
d) 2
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.         unsigned int x : 7;
  5.         unsigned int y : 2;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 110;
  11.         p.y = 2;
  12.         printf("%d\n", p.x);
  13.     }

a) Compile time error
b) 110
c) Depends on the standard
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         unsigned int x : 1;
  5.         unsigned int y : 1;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p;
  10.         p.x = 1;
  11.         p.y = 2;
  12.         printf("%d\n", p.y);
  13.     }

a) 1
b) 2
c) 0
d) Depends on the compiler
View Answer

Answer: c
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.