Session 8
o
Union
o
Preprocessor
directives
1. #include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
- A. 3,
2, 515
- B. 515,
2, 3
- C. 3,
2, 5
- D. 515,
515, 4
Answer: a
Explanation:
The system will allocate 2
bytes for the union.
The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.
2. . Which of the following comment about Union is false?
[A] Union is a structure
whose members share same memory area
[B] The compiler will keep
track of what type of information is currently stored
[C] Only one of the members
of union can be assigned a value at particular time
[D] Size allocated for
Union is the size of its member needing the maximum storage
Answer: B. The compiler will
keep track of what type of information is currently stored
Explanation:
Union is similar to structure the only
difference is the way the memory allocated to structure and union
3. 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
4. Comment on the following union
declaration?
union temp
{
int a;
float b;
char c;
};
union temp s = {1,2.5,’A'}; //REF LINE
Which member of the union will be active after REF LINE?
union temp
{
int a;
float b;
char c;
};
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
5. What would be the size of the
following union declaration?
union uTemp
{
double a;
int b[10];
char c;
}u;
(Assuming size of double = 8, size of int = 4, size of char = 1)
union uTemp
{
double a;
int b[10];
char c;
}u;
(Assuming size of double = 8, size of int = 4, size of char = 1)
A. 4
B. 8
C. 40
D. 80
Answer:
c
Explanation:
int is declared as an array of size 10. So total size will be 10 x 4=40 which
is the highest of all.
6. What type of data
is holded by variable u int this C code?
1. #include <stdio.h>
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
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
Explanation: Size of
the union is determined by the size of the largest member.
7.Members of a union
are accessed as________________.
a) union-name.member
b) union-pointer->member
c) Both a & b
d) None of the mentioned
a) union-name.member
b) union-pointer->member
c) Both a & b
d) None of the mentioned
Answer:c
8. What is the output of this C code?
1. #include <stdio.h>
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]
a) *symtab[i].u.sval
b) symtab[i].u.sval[0]
c) You cannot have
union inside structure
d) Both a & b
d) Both a & b
Answer:d
9.What is the output of this C code(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
b) 8
c) 4
d) 32
Answer:c
10. What is the output of this 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
b) Compile time error
c) 16
d) 5
Answer: d
11. What is the output of this C code?
1. #include <stdio.h>
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)
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
Answer: d
12. What is the output of this C code?
1. #include <stdio.h>
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)
View Answer
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
View Answer
Answer: c
13. Bit feilds can be used in a union
A. true B. False
Answer: B
14. What is the output of this C code?
1. #include <stdio.h>
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
b) 97
c) a
d) Depends on the standard
Answer: b
15. What is the output of this C code?
1. #include <stdio.h>
2. union p
3. {
4. int x;
5. float y;
6. }p;
7. int main()
8. {
9. p.x = 10;
10. printf("%f\n", p.y);
11. }
a) Compile time error
b) Implementation dependent
C) 10.000000
d) 0.000000
b) Implementation dependent
C) 10.000000
d) 0.000000
Answer: b
Explanation: Depending
on the compiler the value to be printed varies.
16. 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
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
17. If we initialize
one element of a union it also initializes other elements of a union.
A. true B. False
Answer: A
18.
What will be the output of the
program ?
#include<stdio.h>
int
main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A. 10
B. 20
C. 30
D. 0
Answer: Option B
Explanation:
Because union
can initialize only one variable at a time. It overwrites the memory with
binary value of 20 where it was initialized with binary value of 10 before.
It takes the last initialized value of its member variables.
18.
Answer: B
Explanation: Because ++ or -- cannot be done on enum value.
19.
20.
21. A union
cannot be nested inside a structure
A. True B. False
Answer: B
22. Nested
unions are allowed
A. True B. False
Answer: A
23. Union
elements can be of different sizes
A. True B. False
Answer: A
24.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
No comments:
Post a Comment