250+ TOP MCQs on Unions and Answers

C Objective Questions on “Unions”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Unions” along with answers, explanations and/or solutions:

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

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

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
Answer: d
Clarification: None.

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

  1.     #include 
  2.     union
  3.     {
  4.         int x;
  5.         char y;
  6.     }p;
  7.     int main()
  8.     {
  9.         p.y = 60;
  10.         printf("%dn", sizeof(p));
  11.     }

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
Answer: c
Clarification: None.

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

  1.     #include 
  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("%dn", p.y);
  13.     }

a) Compile time error
b) Depends on the compiler
c) 60
d) Undefined behaviour
Answer: c
Clarification: None.

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

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

a) Compile time error
b) 97
c) a
d) 1
Answer: d
Clarification: None.

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

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

a) Compile time error
b) 97
c) a
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  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("%fn", p.y);
  12.     }

a) Compile time error
b) Implementation dependent
c) 10.000000
d) 0.000000
Answer: b
Clarification: 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
Answer: b
Clarification: None.

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

  1.     #include 
  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
Answer: c
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *