Here is a listing of C Objective Questions on “Unions” along with answers, explanations and/or solutions:
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
Answer: c
Clarification: None.
2. Which member of the union will be active after REF LINE in the following C code?
-
#include
-
union temp
-
{
-
int a;
-
float b;
-
char c;
-
};
-
union temp s = {1,2.5,’A’}; //REF LINE
a) a
b) b
c) c
d) Such declaration are illegal
Answer: a
Clarification: None.
3. What would be the size of the following union declaration? (Assuming size of double = 8, size of int = 4, size of char = 1)
-
#include
-
union uTemp
-
{
-
double a;
-
int b[10];
-
char c;
-
}u;
a) 4
b) 8
c) 40
d) 80
Answer: c
Clarification: None.
4. What type of data is holded by variable u int in the following C code?
-
#include
-
union u_tag
-
{
-
int ival;
-
float fval;
-
char *sval;
-
} 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
Answer: a
Clarification: 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
Answer: c
Clarification: None.
6. In the following C code, we can access the 1st character of the string sval by using _______
-
#include
-
struct
-
{
-
char *name;
-
union
-
{
-
char *sval;
-
} u;
-
} 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].
Answer: d
Clarification: None.
7. What will be the output of the following C code (Assuming size of int and float is 4)?
-
#include
-
union
-
{
-
int ival;
-
float fval;
-
} u;
-
void main()
-
{
-
printf("%d", sizeof(u));
-
}
a) 16
b) 8
c) 4
d) 32
Answer: c
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
union stu
-
{
-
int ival;
-
float fval;
-
};
-
void main()
-
{
-
union stu r;
-
r.ival = 5;
-
printf("%d", r.ival);
-
}
a) 9
b) Compile time error
c) 16
d) 5
Answer: d
Clarification: None.