C Programming Questions and Answers – Unions – 2

«
»

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

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

advertisement

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

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

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

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

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

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
View Answer

Answer: c
Explanation: None.
advertisement

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

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

a) Compile time error
b) Depends on the compiler
c) 60
d) Undefined behaviour
View Answer

Answer: c
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     union p
  3.     {
  4.         int x;
  5.         char y;
  6.     }k = {1, 97};
  7.     int main()
  8.     {
  9.         printf("%d\n", k.y);
  10.     }

a) Compile time error
b) 97
c) a
d) 1
View Answer

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  2.     union p
  3.     {
  4.         int x;
  5.         char y;
  6.     }k = {.y = 97};
  7.     int main()
  8.     {
  9.         printf("%d\n", k.y);
  10.     }

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

Answer: b
Explanation: None.

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

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

a) Compile time error
b) Implementation dependent
c) 10.000000
d) 0.000000
View Answer

Answer: b
Explanation: None.

7. Which of the following share a similarity in syntax?

1. Union, 2. Structure, 3. Arrays and 4. Pointers

a) 3 and 4
b) 1 and 2
c) 1 and 3
d) 1, 3 and 4
View Answer

Answer: b
Explanation: None.

8. What will be the output of the following C code? (Assuming size of char = 1, int = 4, double = 8)

  1.     #include <stdio.h>
  2.     union utemp
  3.     {
  4.         int a;
  5.         double b;
  6.         char c;
  7.     }u;
  8.     int main()
  9.     {
  10.         u.c = 'A';
  11.         u.a = 1;
  12.         printf("%d", sizeof(u));
  13.     }

a) 1
b) 4
c) 8
d) 13
View Answer

Answer: c
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.