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?
-
#include
-
union
-
{
-
int x;
-
char y;
-
}p;
-
int main()
-
{
-
p.x = 10;
-
printf("%dn", sizeof(p));
-
}
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?
-
#include
-
union
-
{
-
int x;
-
char y;
-
}p;
-
int main()
-
{
-
p.y = 60;
-
printf("%dn", sizeof(p));
-
}
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?
-
#include
-
union p
-
{
-
int x;
-
char y;
-
};
-
int main()
-
{
-
union p p, b;
-
p.y = 60;
-
b.x = 12;
-
printf("%dn", p.y);
-
}
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?
-
#include
-
union p
-
{
-
int x;
-
char y;
-
}k = {1, 97};
-
int main()
-
{
-
printf("%dn", k.y);
-
}
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?
-
#include
-
union p
-
{
-
int x;
-
char y;
-
}k = {.y = 97};
-
int main()
-
{
-
printf("%dn", k.y);
-
}
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?
-
#include
-
union p
-
{
-
int x;
-
float y;
-
};
-
int main()
-
{
-
union p p, b;
-
p.x = 10;
-
printf("%fn", p.y);
-
}
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)
-
#include
-
union utemp
-
{
-
int a;
-
double b;
-
char c;
-
}u;
-
int main()
-
{
-
u.c = 'A';
-
u.a = 1;
-
printf("%d", sizeof(u));
-
}
a) 1
b) 4
c) 8
d) 13
Answer: c
Clarification: None.