5.3.1 - Overview of Unions and Structure vs Union.
1. What is a union in C?
Correct Answer: (A) A user-defined data type that stores different types of data in the same memory location
2. How much memory does a union occupy?
Correct Answer: (B) Equal to the size of the largest member
3. Which keyword is used to declare a union in C?
Correct Answer: (B) union
4. How are union members accessed in C?
Correct Answer: (C) Using the dot (.) operator
5. Which of the following is a correct way to declare a union?
Correct Answer: (B) union example { int a; float b; };
6. What happens when you assign a value to a member of a union?
Correct Answer: (B) Only the last assigned member holds a valid value
7. How does the memory allocation for a union differ from that of a structure?
Correct Answer: (C) Union allocates memory for only one member at a time
8. Which is true regarding unions in C?
Correct Answer: (B) Only one member of a union can store a value at a time
9. What is the size of the union below if char is 1 byte, int is 4 bytes, and float is 4 bytes?
union example { char ch; int a; float b; };
Correct Answer: (D) 4 bytes
10. What is the primary advantage of using a union over a structure?
Correct Answer: (A) Reduced memory usage
11. How can you declare a variable for a union named data in C?
Correct Answer: (A) union data d;
12. What will be the output of the following code if char is 1 byte and int is 4 bytes?
union example { char ch; int a; }; union example ex; printf("%lu", sizeof(ex));
Correct Answer: (B) 4
13. Which of the following is a valid difference between unions and structures?
Correct Answer: (A) Union members share the same memory, while structure members do not
14. Can a union store multiple values simultaneously?
Correct Answer: (B) No
15. Which statement is true about the following union?
union myUnion { int a; float b; };
Correct Answer: (B) myUnion can store either an int or a float, but not both at the same time
16. Which is the correct syntax to access a union member?
Correct Answer: (A) union_name.member_name
17. What is the size of the following union?
union myUnion { double d; char ch[5]; };
Correct Answer: (B) 8 bytes
18. In a union, which member is initialized last in memory?
Correct Answer: (B) The member assigned the last value
19. What is the correct output of the following code?
union example { int a; float b; }; union example ex; ex.a = 10; ex.b = 3.14; printf("%d", ex.a);
Correct Answer: (C) Undefined behavior
20. Which of the following operations is NOT valid for unions in C?
Correct Answer: (D) Storing multiple members simultaneously