5.3 - Unions.


Back Button Home

5.3.1 - Overview of Unions and Structure vs Union.


1. What is a union in C?
  • (A) A user-defined data type that stores different types of data in the same memory location
  • (B) A pre-defined data type
  • (C) A type used to create arrays
  • (D) A structure within a class
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?
  • (A) Equal to the sum of all its members
  • (B) Equal to the size of the largest member
  • (C) Equal to half the size of all its members
  • (D) Equal to the size of the first member
Correct Answer: (B) Equal to the size of the largest member
3. Which keyword is used to declare a union in C?
  • (A) struct
  • (B) union
  • (C) enum
  • (D) typedef
Correct Answer: (B) union
4. How are union members accessed in C?
  • (A) Using the arrow (->) operator
  • (B) Using the colon (:) operator
  • (C) Using the dot (.) operator
  • (D) Using the star (*) operator
Correct Answer: (C) Using the dot (.) operator
5. Which of the following is a correct way to declare a union?
  • (A) union { int a; float b; };
  • (B) union example { int a; float b; };
  • (C) union example(int a, float b);
  • (D) example union { int a; float b; };
Correct Answer: (B) union example { int a; float b; };
6. What happens when you assign a value to a member of a union?
  • (A) All members of the union get updated
  • (B) Only the last assigned member holds a valid value
  • (C) All members of the union are allocated memory
  • (D) All values in the union are saved
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?
  • (A) Union uses more memory than a structure
  • (B) Structure uses less memory than a union
  • (C) Union allocates memory for only one member at a time
  • (D) Structure and union both allocate the same memory
Correct Answer: (C) Union allocates memory for only one member at a time
8. Which is true regarding unions in C?
  • (A) Multiple members of a union can store values simultaneously
  • (B) Only one member of a union can store a value at a time
  • (C) Union members have individual memory locations
  • (D) All members of a union can be accessed at once
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;
                };
                
  • (A) 1 byte
  • (B) 4 bytes
  • (C) 9 bytes
  • (D) 4 bytes
Correct Answer: (D) 4 bytes
10. What is the primary advantage of using a union over a structure?
  • (A) Reduced memory usage
  • (B) Faster access to members
  • (C) Ability to store more data
  • (D) Improved program readability
Correct Answer: (A) Reduced memory usage
11. How can you declare a variable for a union named data in C?
  • (A) union data d;
  • (B) union d data;
  • (C) data union d;
  • (D) d union data;
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));
                
  • (A) 1
  • (B) 4
  • (C) 5
  • (D) 8
Correct Answer: (B) 4
13. Which of the following is a valid difference between unions and structures?
  • (A) Union members share the same memory, while structure members do not
  • (B) Structures are faster than unions
  • (C) Union can store more data than a structure
  • (D) Structures are declared using union
Correct Answer: (A) Union members share the same memory, while structure members do not
14. Can a union store multiple values simultaneously?
  • (A) Yes
  • (B) No
  • (C) Only if they are the same type
  • (D) Only for floating-point types
Correct Answer: (B) No
15. Which statement is true about the following union?
                union myUnion {
                    int a;
                    float b;
                };
                
  • (A) myUnion can store both an int and a float at the same time
  • (B) myUnion can store either an int or a float, but not both at the same time
  • (C) myUnion will allocate memory for both a and b
  • (D) myUnion can only store an integer
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?
  • (A) union_name.member_name
  • (B) member_name.union_name
  • (C) union_name->member_name
  • (D) union.member_name
Correct Answer: (A) union_name.member_name
17. What is the size of the following union?
                union myUnion {
                    double d;
                    char ch[5];
                };
                
  • (A) 5 bytes
  • (B) 8 bytes
  • (C) 10 bytes
  • (D) 13 bytes
Correct Answer: (B) 8 bytes
18. In a union, which member is initialized last in memory?
  • (A) The first declared member
  • (B) The member assigned the last value
  • (C) The largest member
  • (D) The smallest member
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);
                
  • (A) 10
  • (B) 3
  • (C) Undefined behavior
  • (D) Compilation error
Correct Answer: (C) Undefined behavior
20. Which of the following operations is NOT valid for unions in C?
  • (A) Assigning a value to a union member
  • (B) Accessing union members using a dot operator
  • (C) Declaring a union variable
  • (D) Storing multiple members simultaneously
Correct Answer: (D) Storing multiple members simultaneously

5.3.2 - Nested Union.


21. What is a nested union in C?
  • (A) A union declared inside a structure
  • (B) A union declared inside another union
  • (C) A union declared inside a function
  • (D) A union with only one member
Correct Answer: (B) A union declared inside another union
22. Which keyword is used to declare a nested union?
  • (A) union
  • (B) struct
  • (C) typedef
  • (D) enum
Correct Answer: (A) union
23. In a nested union, which memory size is allocated for the union?
  • (A) The sum of all members' sizes
  • (B) The size of the largest member in the entire hierarchy
  • (C) Only the size of the outer union
  • (D) Only the size of the inner union
Correct Answer: (B) The size of the largest member in the entire hierarchy
24. What is the benefit of using nested unions in C?
  • (A) Allows storing multiple members simultaneously
  • (B) Improves memory efficiency and organizes related data
  • (C) Ensures faster access to union members
  • (D) Increases the complexity of code
Correct Answer: (B) Improves memory efficiency and organizes related data
25. Which of the following is a valid way to access a member of a nested union?
  • (A) outerUnion.innerUnion.member
  • (B) innerUnion.outerUnion.member
  • (C) outerUnion->innerUnion->member
  • (D) innerUnion.member
Correct Answer: (A) outerUnion.innerUnion.member
26. In the following code, what is the type of myUnion.innerUnion.innerChar?
                union OuterUnion {
                    int outerInt;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                
  • (A) int
  • (B) char
  • (C) double
  • (D) float
Correct Answer: (B) char
27. What is a key difference between structures and unions in C?
  • (A) Unions store all members simultaneously
  • (B) Structures allocate memory for each member, unions allocate memory for the largest member
  • (C) Structures are more memory efficient than unions
  • (D) Unions cannot contain nested members
Correct Answer: (B) Structures allocate memory for each member, unions allocate memory for the largest member
28. How are members of a union accessed?
  • (A) Using the dot (.) operator
  • (B) Using the arrow (->) operator
  • (C) Using the ampersand (&) operator
  • (D) Using the colon (:) operator
Correct Answer: (A) Using the dot (.) operator
29. What happens if you assign a value to the outerInt and then access innerChar in the following union?
                union OuterUnion {
                    int outerInt;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                
  • (A) Both values will be accessible
  • (B) Only innerChar will be valid
  • (C) Only outerInt will be valid
  • (D) Undefined behavior
Correct Answer: (D) Undefined behavior
30. Which of the following demonstrates the correct way to initialize a nested union?
  • (A) union OuterUnion myUnion = {10, {'A'}};
  • (B) union OuterUnion myUnion = {.outerInt = 10};
  • (C) union OuterUnion myUnion = {10};
  • (D) union OuterUnion myUnion = {A, 3.14};
Correct Answer: (B) union OuterUnion myUnion = {.outerInt = 10};
31. In the given code, which member of the outer union takes up the most memory?
                union OuterUnion {
                    int outerInt;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                
  • (A) outerInt
  • (B) innerChar
  • (C) innerDouble
  • (D) Both innerChar and outerInt
Correct Answer: (C) innerDouble
32. Which of the following is an advantage of using unions in C?
  • (A) It provides direct memory access
  • (B) It improves memory efficiency by sharing memory among members
  • (C) It guarantees faster execution of programs
  • (D) It allows simultaneous access to all members
Correct Answer: (B) It improves memory efficiency by sharing memory among members
33. How many members of a nested union can be active at the same time?
  • (A) One from the outer union and one from the inner union
  • (B) All members
  • (C) Only one member at a time from either the inner or outer union
  • (D) Two members, one from each union
Correct Answer: (C) Only one member at a time from either the inner or outer union
34. In the following example, what will the size of OuterUnion be?
                union OuterUnion {
                    int outerInt;
                    float outerFloat;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                
  • (A) 1 byte
  • (B) 4 bytes
  • (C) 8 bytes
  • (D) 12 bytes
Correct Answer: (C) 8 bytes
35. Which member of the nested union will be accessed in this code?
                union OuterUnion myUnion;
                myUnion.innerUnion.innerChar = 'B';
                printf("%c", myUnion.innerUnion.innerChar);
                
  • (A) outerInt
  • (B) outerFloat
  • (C) innerChar
  • (D) innerDouble
Correct Answer: (C) innerChar
36. What happens to the value of the outer union when you assign a value to a member of the inner union?
  • (A) The outer union's value remains unchanged
  • (B) The outer union's value is lost
  • (C) Both values are retained
  • (D) Only the outer union is valid
Correct Answer: (B) The outer union's value is lost
37. In the following code, which member is part of the inner union?
                union OuterUnion {
                    int outerInt;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                
  • (A) outerInt
  • (B) innerChar
  • (C) outerFloat
  • (D) outerUnion
Correct Answer: (B) innerChar
38. What does the following code print?
                union OuterUnion {
                    int outerInt;
                    union InnerUnion {
                        char innerChar;
                        double innerDouble;
                    } innerUnion;
                };
                union OuterUnion myUnion;
                myUnion.innerUnion.innerDouble = 3.14;
                printf("%f", myUnion.innerUnion.innerDouble);
                
  • (A) 3.14
  • (B) Compilation error
  • (C) Undefined behavior
  • (D) 0.00
Correct Answer: (A) 3.14
39. How is memory allocated when a union is nested within another union?
  • (A) The memory for both unions is summed
  • (B) The largest member of both unions determines the total memory
  • (C) The outer union’s memory is used for the inner union
  • (D) Only one union uses memory
Correct Answer: (B) The largest member of both unions determines the total memory
40. Can a union be nested inside a structure in C?
  • (A) Yes
  • (B) No
  • (C) Only in C++
  • (D) Only if both are the same type
Correct Answer: (A) Yes

5.3.3 - Array of Union.


41. What is an array of unions in C?
  • (A) An array that stores different data types in each element
  • (B) An array where each element is a union that can hold different data types
  • (C) A union that stores an array of the same data type
  • (D) A union that stores different data types simultaneously
Correct Answer: (B) An array where each element is a union that can hold different data types
42. How is an array of unions declared in C?
  • (A) union MyUnion array[5];
  • (B) struct MyUnion array[5];
  • (C) union MyUnion[5];
  • (D) union MyUnion *array[5];
Correct Answer: (A) union MyUnion array[5];
43. In an array of unions, each element in the array:
  • (A) Can store multiple values simultaneously
  • (B) Shares memory with all other elements
  • (C) Can store one of several data types defined in the union
  • (D) Is always of the same data type
Correct Answer: (C) Can store one of several data types defined in the union
44. What is the memory size allocated for each element in an array of unions?
  • (A) The size of the smallest member in the union
  • (B) The size of the largest member in the union
  • (C) The sum of the sizes of all members in the union
  • (D) A fixed size determined by the array
Correct Answer: (B) The size of the largest member in the union
45. Which of the following operations is valid when using an array of unions?
  • (A) Accessing multiple union members at the same time
  • (B) Assigning values to different members in different array indices
  • (C) Using different data types simultaneously in the same array element
  • (D) Accessing union members without specifying the index
Correct Answer: (B) Assigning values to different members in different array indices
46. In the following code, what type is stored in arrayOfUnions[1]?
                union MyUnion {
                    int intValue;
                    float floatValue;
                    char charValue;
                };
                union MyUnion arrayOfUnions[5];
                arrayOfUnions[1].floatValue = 3.14;
                
  • (A) int
  • (B) float
  • (C) char
  • (D) Undefined behavior
Correct Answer: (B) float
47. What happens if you assign values to two members of a union at the same index in an array of unions?
  • (A) Both values will be stored
  • (B) Only the first value is retained
  • (C) Only the second value is retained
  • (D) Undefined behavior
Correct Answer: (C) Only the second value is retained
48. What is the benefit of using arrays of unions?
  • (A) They allow storing multiple data types in a single element of the array
  • (B) They provide memory efficiency by allocating space for only the largest union member
  • (C) They automatically check the type of each element in the array
  • (D) They store each member of the union at a separate memory location
Correct Answer: (B) They provide memory efficiency by allocating space for only the largest union member
49. How do you access an element of a union inside an array of unions?
  • (A) array[i].unionMember
  • (B) unionMember.array[i]
  • (C) array->unionMember[i]
  • (D) array->i.unionMember
Correct Answer: (A) array[i].unionMember
50. In the following example, what value will be printed for arrayOfUnions[2].charValue?
                union MyUnion {
                    int intValue;
                    float floatValue;
                    char charValue;
                };
                union MyUnion arrayOfUnions[5];
                arrayOfUnions[2].charValue = 'A';
                printf("%c", arrayOfUnions[2].charValue);
                
  • (A) A
  • (B) 0
  • (C) Undefined behavior
  • (D) Compilation error
Correct Answer: (A) A
51. Can an array of unions contain elements with different data types?
  • (A) Yes, each element of the array can store different types of data at different times
  • (B) No, all elements must store the same data type
  • (C) Yes, but only for integer types
  • (D) No, arrays cannot be used with unions
Correct Answer: (A) Yes, each element of the array can store different types of data at different times
52. Which of the following statements is true about arrays of unions?
  • (A) An array of unions always stores the largest member's type
  • (B) Only one member of the union can be used at a time in each element
  • (C) All members of the union can be used simultaneously in different array elements
  • (D) An array of unions stores the sum of the sizes of all union members
Correct Answer: (B) Only one member of the union can be used at a time in each element
53. In the following code, what is the output for items[1].price?
                union ItemAttributes {
                    int weight;
                    float price;
                    char category;
                };
                union ItemAttributes items[3];
                items[1].price = 19.99;
                printf("%.2f", items[1].price);
                
  • (A) 19.99
  • (B) 0.00
  • (C) Undefined behavior
  • (D) Compilation error
Correct Answer: (A) 19.99
54. How is memory allocated for arrays of unions?
  • (A) Each element uses the memory required by the sum of all members of the union
  • (B) Each element uses the memory required by the largest member of the union
  • (C) Memory is allocated only for the first element
  • (D) Memory allocation is dynamic based on the number of elements in use
Correct Answer: (B) Each element uses the memory required by the largest member of the union
55. Can arrays of unions be passed as function arguments?
  • (A) Yes, arrays of unions can be passed to functions like any other array
  • (B) No, arrays of unions cannot be passed to functions
  • (C) Yes, but only if they contain integer types
  • (D) Yes, but only by reference
Correct Answer: (A) Yes, arrays of unions can be passed to functions like any other array
56. What would happen if you try to print an uninitialized element of an array of unions?
  • (A) It will print a garbage value
  • (B) It will print 0
  • (C) Undefined behavior
  • (D) Compilation error
Correct Answer: (C) Undefined behavior
57. Which of the following correctly initializes an element in an array of unions?
  • (A) array[0] = {10};
  • (B) array[0].intValue = 10;
  • (C) array[0] = {.intValue = 10};
  • (D) array[0] = intValue = 10;
Correct Answer: (B) array[0].intValue = 10;
58. In an array of unions, each element is initialized:
  • (A) Automatically to the first member of the union
  • (B) Manually for each member
  • (C) Using the size of the smallest member
  • (D) Automatically to zero
Correct Answer: (B) Manually for each member
59. Can arrays of unions be dynamically allocated?
  • (A) Yes, using malloc
  • (B) No, arrays of unions must be statically allocated
  • (C) Only in C++
  • (D) Only if the array contains more than 5 elements
Correct Answer: (A) Yes, using malloc
60. What is a key difference between arrays of unions and arrays of structures?
  • (A) Arrays of unions store all members simultaneously
  • (B) Arrays of unions allocate memory based on the largest member, arrays of structures allocate memory for all members
  • (C) Arrays of structures cannot store different data types
  • (D) Arrays of structures are faster to access
Correct Answer: (B) Arrays of unions allocate memory based on the largest member, arrays of structures allocate memory for all members

5.3.4 - Passing Union as Arguments in Functions.


61. How can you pass a union to a function in C?
  • (A) By passing the entire union
  • (B) By passing specific union members
  • (C) By passing a pointer to the union
  • (D) All of the above
Correct Answer: (D) All of the above
62. What happens when you pass a union by value to a function?
  • (A) The original union is modified
  • (B) A copy of the union is passed, and the original remains unchanged
  • (C) The function accesses the union directly
  • (D) The union is passed as a pointer
Correct Answer: (B) A copy of the union is passed, and the original remains unchanged
63. What is the syntax to pass a specific member of a union to a function?
  • (A) functionName(union.member);
  • (B) functionName(&union.member);
  • (C) functionName(*union.member);
  • (D) functionName.union.member;
Correct Answer: (A) functionName(union.member);
64. Why would you pass a union by reference to a function?
  • (A) To modify the original union in the calling function
  • (B) To save memory
  • (C) To prevent the union from being copied
  • (D) All of the above
Correct Answer: (D) All of the above
65. Which operator is used to access members of a union when passed as a pointer to a function?
  • (A) . (dot operator)
  • (B) * (dereference operator)
  • (C) -> (arrow operator)
  • (D) & (address operator)
Correct Answer: (C) -> (arrow operator)
66. What is the result of passing a union pointer to a function?
  • (A) The function works with a copy of the union
  • (B) The original union can be modified
  • (C) The union is passed by value
  • (D) The function can only read the union
Correct Answer: (B) The original union can be modified
67. How can you ensure type safety when passing union members to a function?
  • (A) By always passing the entire union
  • (B) By specifying the correct data type in the function argument
  • (C) By using type casting in the function
  • (D) Type safety is automatic in unions
Correct Answer: (B) By specifying the correct data type in the function argument
68. What happens if you pass the wrong union member type to a function?
  • (A) Compilation error
  • (B) Unpredictable behavior
  • (C) The program will automatically cast it
  • (D) The function will print a warning
Correct Answer: (B) Unpredictable behavior
69. How is memory handled when passing a union by value to a function?
  • (A) The function accesses the original memory location
  • (B) A new memory location is created for the union copy
  • (C) The union is passed by reference
  • (D) The union is passed as a pointer
Correct Answer: (B) A new memory location is created for the union copy
70. Which of the following is a valid function signature for passing a union by reference?
  • (A) void function(union MyUnion u);
  • (B) void function(union MyUnion *u);
  • (C) void function(&union MyUnion u);
  • (D) void function(*union MyUnion u);
Correct Answer: (B) void function(union MyUnion *u);
71. When passing a union pointer to a function, how can you modify a member of the union?
  • (A) By using the . (dot) operator
  • (B) By using the -> (arrow) operator
  • (C) By using the * (dereference) operator
  • (D) By using the & (address) operator
Correct Answer: (B) By using the -> (arrow) operator
72. What is the benefit of passing specific union members rather than the entire union?
  • (A) It saves memory and improves performance
  • (B) It allows the function to access all union members
  • (C) It prevents modifications to the original union
  • (D) It simplifies type checking
Correct Answer: (A) It saves memory and improves performance
73. Which of the following is true about passing unions by value to a function?
  • (A) The union is modified within the function
  • (B) The original union remains unchanged after the function call
  • (C) The union is automatically passed by reference
  • (D) The union is copied only if it's a large union
Correct Answer: (B) The original union remains unchanged after the function call
74. How do you pass a union as an argument to a function if you want to avoid copying the union?
  • (A) Pass a pointer to the union
  • (B) Use the & operator to pass by reference
  • (C) Pass only specific union members
  • (D) Both A and B
Correct Answer: (D) Both A and B
75. Which keyword is used to define a union in C?
  • (A) struct
  • (B) class
  • (C) union
  • (D) enum
Correct Answer: (C) union
76. What is the primary difference between passing a structure and a union to a function?
  • (A) A union stores only one value at a time, while a structure stores all its members
  • (B) A union uses less memory
  • (C) A structure passes all its members, while a union passes only the largest member
  • (D) Both pass their members the same way
Correct Answer: (A) A union stores only one value at a time, while a structure stores all its members
77. How do you pass multiple members of a union to a function at the same time?
  • (A) Pass the union itself by reference
  • (B) Pass a structure that contains the union
  • (C) Pass each member individually
  • (D) Unions cannot pass multiple members simultaneously
Correct Answer: (D) Unions cannot pass multiple members simultaneously
78. What is a possible drawback of passing union members to a function?
  • (A) Loss of flexibility in the function
  • (B) Loss of type safety
  • (C) The union may be modified unexpectedly
  • (D) It can result in memory leaks
Correct Answer: (B) Loss of type safety
79. How can you prevent modification of a union passed to a function?
  • (A) Pass the union by value
  • (B) Pass the union by reference
  • (C) Pass a constant pointer to the union
  • (D) Use a const union
Correct Answer: (C) Pass a constant pointer to the union
80. What happens when a union is passed by reference to a function?
  • (A) The function receives a copy of the union
  • (B) The function works directly on the original union
  • (C) The union is passed as a value
  • (D) Only specific members are passed
Correct Answer: (B) The function works directly on the original union