C Programming Multiple choice Questions and Answers on Unions for Freshers

1. Size of a union is determined by 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
Answer: c
2. Comment on the following union declaration?
  1.     #include 
  2.     union temp
  3.     {
  4.         int a;
  5.         float b;
  6.         char c;
  7.     };
    union temp s = {1,2.5,’A’}; //REF LINE
    Which member of the union will be active after REF LINE?

a) a
b) b
c) c
d) Such declaration are illegal
Answer: a
3. What would be the size of the following union declaration?
  1.     #include 
  2.     union uTemp
  3.     {
  4.         double a;
  5.         int b[10];
  6.         char c;
  7.     }u;
    (Assuming size of double = 8, size of int = 4, size of char = 1)

a) 4
b) 8
c) 40
d) 80
Answer: c
4. What type of data is holded by variable u int this C code?
  1.     #include 
  2.     union u_tag
  3.     {
  4.         int ival;
  5.         float fval;
  6.         char *sval;
  7.     } u;
    The variable u here

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
Answer: a
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
Answer: c
6. What is the output of this C code?
  1.     #include 
  2.     struct
  3.     {
  4.         char *name;
  5.         union
  6.         {
  7.             char *sval;
  8.         } u;
  9.     } symtab[10];
    the first character of the string sval by either of

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].
Answer: d
7. What is the output of this C code(size of int and float is 4)?
  1.     #include 
  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
Answer: c
8. What is the output of this C code?
  1.     #include 
  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
Answer: d
9. What is the output of this 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("%d\n", sizeof(p));
  11.     }
a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
Answer: d
10. What is the output of this 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("%d\n", sizeof(p));
  11.     }
a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
Answer: c
11. What is the output of this 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("%d\n", p.y);
  13.     }
a) Compile time error
b) Depends on the compiler
c) 60
d) Undefined behaviour
Answer: c
12. What is the output of this C code?
  1.     #include 
  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
Answer: d
13. What is the output of this C code?
  1.     #include 
  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
Answer: b
14. What is the output of this 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("%f\n", p.y);
  12.     }
a) Compile time error
b) Implementation dependent
C) 10.000000
d) 0.000000
Answer: b
15. 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
16. What is the output of this C code?
  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.     }
    The output will be: (Assuming size of char = 1, int = 4, double = 8)
a) 1
b) 4
c) 8
d) 13
Answer: c

Related

Multiple Choice Questions 5525498223482087453

Post a Comment

emo-but-icon

item