C Programming Questions and Answers – Unions – 1

«
»

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

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

advertisement

1. The size of a union is determined by the size of the __________
a) First member in the union
b) Last member in the union
c) Biggest member in the union
d) Sum of the sizes of all members
View Answer

Answer: c
Explanation: None.

2. Which member of the union will be active after REF LINE in the following C code?

advertisement
advertisement
  1.     #include <stdio.h>
  2.     union temp
  3.     {
  4.         int a;
  5.         float b;
  6.         char c;
  7.     };
  8.     union temp s = {1,2.5,’A’}; //REF LINE

a) a
b) b
c) c
d) Such declaration are illegal
View Answer

Answer: a
Explanation: None.
advertisement

3. What would be the size of the following union declaration? (Assuming size of double = 8, size of int = 4, size of char = 1)

  1.     #include <stdio.h>
  2.     union uTemp
  3.     {
  4.         double a;
  5.         int b[10];
  6.         char c;
  7.     }u;

a) 4
b) 8
c) 40
d) 80
View Answer

Answer: c
Explanation: None.
advertisement

4. What type of data is holded by variable u int in the following C code?

advertisement
  1.     #include <stdio.h>
  2.     union u_tag
  3.     {
  4.         int ival;
  5.         float fval;
  6.         char *sval;
  7.     } u;

a) Will be large enough to hold the largest of the three types;
b) Will be large enough to hold the smallest of the three types;
c) Will be large enough to hold the all of the three types;
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

5. Members of a union are accessed as________________
a) union-name.member
b) union-pointer->member
c) both union-name.member & union-pointer->member
d) none of the mentioned
View Answer

Answer: c
Explanation: None.

6. In the following C code, we can access the 1st character of the string sval by using _______

  1.     #include <stdio.h>
  2.     struct
  3.     {
  4.         char *name;
  5.         union
  6.         {
  7.             char *sval;
  8.         } u;
  9.     } symtab[10];

a) *symtab[i].u.sval
b) symtab[i].u.sval[0].
c) You cannot have union inside structure
d) Both *symtab[i].u.sval & symtab[i].u.sval[0].
View Answer

Answer: d
Explanation: None.

7. What will be the output of the following C code (Assuming size of int and float is 4)?

  1.     #include <stdio.h>
  2.     union
  3.     {
  4.         int ival;
  5.         float fval;
  6.     } u;
  7.     void main()
  8.     {
  9.         printf("%d", sizeof(u));
  10.     }

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

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  2.     union stu
  3.     {
  4.         int ival;
  5.         float fval;
  6.     };
  7.     void main()
  8.     {
  9.         union stu r;
  10.         r.ival = 5;
  11.         printf("%d", r.ival);
  12.     }

a) 9
b) Compile time error
c) 16
d) 5
View Answer

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