Union in C Programming MCQ Questions and Answers
1. Which of the following best describes a union in C?
A) A collection of named bit-fields only
B) A type whose members share the same memory location
C) A type that can hold multiple values simultaneously
D) A function-like macro for grouping variables
Answer: B
2. If a union has members of sizes 4, 8 and 2 bytes, the sizeof the union is guaranteed to be:
A) 14 bytes
B) 8 bytes or greater (at least the largest member)
C) 4 bytes
D) 2 bytes
Answer: B
3. How many members of a union can hold valid values at the same time?
A) All members simultaneously
B) Exactly two members
C) Only one member at a time (the last written)
D) Zero members
Answer: C
4. Given union U { int i; float f; }; what does U u; u.i = 5; do?
A) Stores integer 5 into the memory used by the union
B) Stores float representation of 5.0 into the union and sets both members
C) Initializes only f and leaves i undefined
D) Creates two copies, one for i and one for f
Answer: A
5. Which is TRUE about alignment of a union type?
A) Alignment is always 1 byte regardless of members
B) Alignment equals the maximum alignment requirement among its members
C) Alignment equals sum of alignments of members
D) Alignment is undefined for unions
Answer: B
6. What is the value of sizeof an empty union (with no members) in standard C?
A) 0 bytes
B) 1 byte (minimum object size) or implementation-defined
C) 8 bytes
D) The union cannot be declared empty in standard C
Answer: D
7. Accessing a member of a union other than the one most recently stored into is:
A) Always undefined behavior in every case
B) Sometimes implementation-defined or undefined — depends on type and standard rules
C) Always safe and guaranteed by the C standard
D) Only allowed for pointers to unions
Answer: B
8. Which of the following is the correct syntax to declare a union type named Data?
A) union Data { int x; float y; };
B) Data union { int x; float y; };
C) struct Data { union int x; };
D) union { Data int x; };
Answer: A
9. How do you define and simultaneously declare a variable d of union Data?
A) union Data d;
B) Data union d;
C) union d Data;
D) struct Data d;
Answer: A
10. Can union members be arrays? Example: union U { char s[10]; int x; }; — is this legal?
A) No — array members are not allowed in unions
B) Yes — arrays as union members are allowed
C) Only if arrays are static
D) Only if arrays are pointers
Answer: B
11. What happens to the previously stored member when you assign a new value to a different union member?
A) It remains valid and unchanged
B) Its value may be overwritten (no longer reliable)
C) The union stores both values separately
D) The compiler raises a runtime exception
Answer: B
12. Can a union contain members of non-POD (complex) types in C? (Consider plain C, not C++)
A) C has no concept of POD; unions can contain any object type valid in C (including structs)
B) No — unions cannot contain structs in C
C) Only pointers may be inside a union
D) Only integer types are allowed in unions
Answer: A
13. Which statement is true about using typedef with unions?
A) typedef cannot be used with union types
B) You can create an alias: typedef union { int x; } U; then use U u;
C) typedef changes the memory layout of the union
D) typedef makes union members private
Answer: B
14. Consider union { int x; double y; } u = { .y = 3.14 };. What is the active member?
A) x is active
B) y is active
C) Both x and y are active
D) No active member until explicitly assigned later
Answer: B
15. Which of these is a common practical use-case for unions?
A) Enforcing type safety at compile time
B) Memory-efficient representation of multiple interpretations (type punning)
C) Run-time polymorphism like OOP virtual tables
D) Replacing all uses of structs in a program
Answer: B
16. Can you have a pointer to a union type? Example: union U *p;
A) No — pointers to unions are illegal
B) Yes — pointers to unions are allowed like any other object pointers
C) Only const pointers are allowed
D) Only if union members are all same size
Answer: B
17. What is an anonymous union (conceptually)?
A) A union without a tag name declared inside another scope, so members are accessed as if they belonged to surrounding scope
B) A union with no members
C) A union declared inside a function only
D) A union with only unnamed members possible in C
Answer: A
18. Which of the following is FALSE?
A) Union members may be initialized at definition using a designated initializer for one member
B) Multiple members can be initialized simultaneously in a union initializer list
C) If you initialize a union with a member, that member becomes active
D) Designated initializers like .x = 5 are allowed for unions in C99 and later
Answer: B
19. Regarding unions and const/volatile qualifiers: can you have a const union variable?
A) No, qualifiers cannot be applied to unions
B) Yes, e.g., const union U u = { .x = 1 }; is valid
C) Only volatile is allowed, not const
D) Only members can be const, not the union object
Answer: B
20. Which of these statements about nested unions is correct?
A) A union cannot contain another union as a member
B) A union may contain another union or a struct as a member
C) Nested unions automatically share members with the parent union
D) Nested unions must be anonymous
Answer: B
21. Given union U { int i; char c; }; union U u; u.c = ‘A’; int x = u.i; — what can be said about x?
A) x is guaranteed to be ASCII code of ‘A’ on all systems
B) x content is implementation-dependent (type-punning effect)
C) x equals 0 always
D) This code does not compile
Answer: B
22. Which is the smallest valid declaration of a union variable?
A) union { int x; } u;
B) union u;
C) union;
D) union u { };
Answer: A
23. Can a union contain bit-field members? Example: union { int a:3; char b; };
A) No — bit-fields are not allowed in unions
B) Yes — bit-field members are allowed in unions
C) Only if bit-field is static
D) Only if union has no other members
Answer: B
24. Which of the following about sizeof a union and its members is true?
A) sizeof(union) is always equal to sizeof its largest member rounded up for alignment
B) sizeof(union) is the sum of all member sizes
C) sizeof(union) is always 1 larger than the largest member
D) sizeof(union) is always equal to sizeof(int)
Answer: A
25. Can union members be pointers to functions? Example: union { void (*fptr)(int); int x; };
A) No — function pointers cannot be members of unions
B) Yes — function pointers can be union members
C) Only if functions are static
D) Only if pointers are cast to void* first
Answer: B
26. What will the following print (assuming 32-bit int and typical little-endian)?
union { int i; char c[4]; } u;
u.i = 1;
printf(“%d\n”, u.c[0]);
A) 0
B) 1
C) Undefined and cannot be printed
D) 255
Answer: B
27. Which of the following is true about initialization of union members at definition?
A) Only the first member may be initialized without using designated initializer
B) You can initialize any member with designated initializer, e.g., = { .c = 5 }
C) Unions cannot be initialized at definition
D) You must call memset to initialize union members
Answer: B
28. Consider union U { int x; float y; } u = { 10 }; Which member is initialized?
A) x (the first member)
B) y (float)
C) Both x and y
D) None — initializer is ambiguous
Answer: A
29. If you memset a union to zero (memset(&u, 0, sizeof u)), which member becomes active according to standard C?
A) The standard guarantees the first member is active with zero value
B) The operation sets raw bytes to zero; active member concept is not changed by memset per se — behavior of reading non-active members is still implementation-dependent
C) memset is illegal on unions
D) memset sets all members to zero and activates all of them
Answer: B
30. Which of these statements about union members with automatic storage duration (local variables) is correct?
A) Union members with automatic storage cannot be used inside functions
B) A local union variable can be declared and assigned like any other object; its lifetime follows automatic storage rules
C) Union members are always static by default
D) You must explicitly allocate unions with malloc inside functions
Answer: B
31. Is it possible to take the address of a union member? Example: &u.x where u is a union variable.
A) No — union members have no address
B) Yes — you can take address of any union member (subject to usual rules)
C) Only the active member has an address
D) Only if union is declared static
Answer: B
32. What is the value of this expression if u is a union and u.i was last assigned 0: ((unsigned char*)&u)[0]?
A) Always 0 (reads first byte)
B) Undefined — pointer cast is illegal
C) Always 1
D) Always -1
Answer: A
33. Which statement concerning unions and functions is true?
A) You cannot pass unions by value to functions in C
B) You may pass unions by value or pointer like other objects (if the ABI supports it)
C) Unions must always be passed via pointer to functions
D) Functions cannot return union types
Answer: B
34. Regarding returning unions from functions in standard C:
A) Functions can return union types (return by value)
B) Functions cannot return unions — only pointers or structs
C) Only inline functions can return unions
D) You must always use global variables to return unions
Answer: A
35. Which of the following about union tag names is correct?
A) union tags live in the same namespace as variables and functions
B) union tags live in the tag namespace and are used as union name unless typedef used
C) Tag names must be unique across the whole program including structs
D) You cannot use the same tag name for a struct and a union
Answer: B
36. What will this code do?
union U { int a; double b; } u;
u.b = 1.23;
printf(“%d\n”, u.a);
A) Print integer representation from same bytes (implementation-dependent)
B) Print 1 always
C) Crash the program guaranteed
D) Refuse to compile
Answer: A
37. Which of these statements is a safe portable use of unions in C?
A) Using unions to reinterpret one type as another relying on implementation-defined byte order
B) Using unions to store one of several different data types and only accessing the active member
C) Modifying one member and immediately reading a different member to inspect bit layout — portable across compilers
D) Using unions to get around strict aliasing rules in every case
Answer: B
38. Can a union member be a flexible array member (e.g., char data[];)?
A) Yes, flexible array members are allowed in unions in standard C
B) No — flexible array members are not allowed as union members (C standard forbids it)
C) Only if the union is the last member of a struct
D) Only in C++
Answer: B
39. Which of the following union declarations is invalid?
A) union U { int x; int x; }; (duplicate member names)
B) union U { int x; double y; };
C) union U { struct { int a; } s; };
D) union U { int *p; void (*f)(void); };
Answer: A
40. If union U { int i; char c; } u = {.c = ‘A’}; what does u.i represent?
A) The same bit-pattern interpreted as an int (implementation-defined)
B) Guaranteed to be integer 65 on all platforms
C) Undefined and guaranteed to crash if read
D) Always zero
Answer: A
41. When embedding a union inside a struct, how does the union affect the struct size?
A) It contributes its sizeof (largest member size and alignment) to the struct, possibly with padding
B) It doubles the struct size
C) It is ignored in sizeof computation
D) It makes the struct uninstantiable
Answer: A
42. Which property differentiates a union from a struct?
A) Union members all start at same memory offset; struct members have distinct offsets
B) Union members are always signed; struct members unsigned
C) Unions require dynamic allocation; structs do not
D) Structs can have functions as members; unions cannot
Answer: A
43. Suppose you declare union U { int i; short s[2]; };. On a platform where sizeof(int)==4 and sizeof(short)==2, which is true?
A) sizeof(U) will be at least 4 bytes
B) sizeof(U) must be 8 bytes always
C) sizeof(U) will be 2 bytes
D) sizeof(U) is unspecified and could be zero
Answer: A
44. Which of the following is allowed inside unions in C?
A) Members with initializers at declaration (in the member declaration)
B) Static members inside union (e.g., static int x;)
C) Function definitions inside unions
D) A member that is itself a struct or union
Answer: D
45. Using a union for type punning (e.g., write u.f and read u.i) is:
A) Universally portable and specified by the C standard in all cases
B) Historically supported, but its behavior can be implementation-defined — common compilers allow it
C) Illegal and causes a compile-time error
D) Only allowed when volatile is used
Answer: B
46. If a union contains a member that requires stricter alignment than other members, what must the compiler do?
A) It must align the union itself to satisfy the strictest member alignment
B) It may ignore alignment needs; behavior undefined
C) It must split the union into multiple objects at different addresses
D) It must pad the member to the sum of alignments
Answer: A
47. Given union U { long l; double d; } and sizeof(long)==8, sizeof(double)==8, what can be said about sizeof(union U)?
A) Exactly 8 bytes (subject to alignment)
B) Exactly 16 bytes
C) Exactly 1 byte
D) Could be 0
Answer: A
48. Can union members have different access specifiers like private or public in C?
A) Yes, union supports public and private members in C
B) No — C does not have access specifiers; that’s a C++ feature
C) Yes, via GCC attributes only
D) Only for anonymous unions
Answer: B
49. Consider union { int x:5; int y:3; } u; — what are x and y?
A) Bit-field members sharing same memory because they are union members
B) Independent integer members that do not overlap
C) Illegal — bit-fields cannot be in unions
D) They both must be unsigned only
Answer: A
50. Which of the following is a potential pitfall when using unions for serialization across different architectures?
A) Endianness differences and differing alignment/size of members
B) Unions always encode data in network byte order
C) Unions enforce identical layout across compilers always
D) Unions automatically convert to text format for serialization
Answer: A
51. Can a union member be declared as an incomplete type (e.g., struct S; used as pointer)?
A) Yes — pointers to incomplete types are allowed as union members
B) No — incomplete types are forbidden in unions
C) Only if the pointer is volatile
D) Only if the incomplete type has been forward-declared and defined before use
Answer: A
52. Suppose union U { int a; char b; } u = { .a = 0 }; Then u.b = ‘Z’; After this, which is true?
A) u.a holds the integer value 90 guaranteed
B) u.a may contain the bit pattern corresponding to ‘Z’ but not guaranteed the integer 90 on all systems
C) Setting u.b also updates a hidden u.a_copy to same value
D) The program will not compile
Answer: B
53. Which of these is a valid reason to use #pragma pack with unions?
A) To control packing and remove unwanted padding if ABI allows
B) To create union methods like in OOP
C) To make unions dynamic in size at runtime
D) To duplicate union members automatically
Answer: A
54. When you free() a pointer to a heap-allocated union, what happens to its members?
A) The memory is deallocated; members no longer valid — accessing them is undefined behavior
B) All members are automatically set to zero before freeing
C) The union’s last active member is preserved in another memory area
D) The members continue to be valid until overwritten by OS
Answer: A
55. Which is true if you declare union U { int i; int j; } u; and set u.i = 10; then read u.j?
A) u.j is guaranteed to be 10 because both members are same type and size
B) It will always be 0
C) It will cause a compile-time error because duplicate types are not allowed
D) It is undefined because members have different names (but some compilers will show same bit pattern)
Answer: A
56. For debugging and portability, which practice is recommended when using unions?
A) Always document which union member is currently active and access only that member
B) Always read different members than last stored to check memory layout
C) Use union members interchangeably without comments
D) Avoid using unions altogether in systems code
Answer: A
57. Which is true about zero-initialization of a union at file scope: union U u = {0};?
A) It sets all bytes of the union to zero, effectively setting the first member to zero (or equivalent)
B) It leaves union uninitialized
C) It initializes all members to zero individually and activates them all
D) It is illegal at file scope
Answer: A
58. When is the common-initial-sequence rule relevant with unions?
A) When union contains two structs that share identical first members, some compilers allow inspecting the common initial sequence safely
B) It is relevant only for bit-fields inside unions
C) It applies to unions with arrays only
D) The C standard forbids any common-initial-sequence behavior with unions
Answer: A
59. Which of the following is NOT allowed as a union member in ISO C?
A) An object type like int
B) A function definition (body)
C) A struct type declared inline
D) A pointer to function
Answer: B
60. Which of these can change the sizeof a union across platforms?
A) Differences in alignment requirements and maximum member size on that platform
B) The union tag name length
C) The order of members only (without changing types) — always changes size
D) The C compiler version only, irrespective of platform ABI
Answer: A
61. Consider a union used for interpreting bytes of a float. What must be considered?
A) Endianness and representation of float (IEEE-754 vs other) — interpretation of bytes is implementation-defined
B) Floats are always 32-bit IEEE-754, so no concern
C) You can always cast union to double safely
D) Floats will be converted to ASCII automatically in unions
Answer: A
62. Can an unnamed (anonymous) union be declared at file scope in C (with common compilers)?
A) Generally yes in many compilers as an extension; standard behavior varies — avoid relying on it unless supported
B) No — anonymous unions are never allowed at file scope
C) Yes — anonymous unions are required by standard C
D) Only if declared inside inline functions
Answer: A
63. Which language feature in C++ differs from C with respect to unions?
A) C++ allows members with non-trivial constructors/destructors in unions under controlled conditions (C++11 and later) — C has no constructors
B) C++ prohibits unions entirely
C) C has classes inside unions; C++ does not
D) C++ unions must be static
Answer: A
64. If you store into one union member and then later store into another, is the value of the first guaranteed to be preserved?
A) Yes, its value is preserved forever
B) No — writing another member may overwrite bytes used by the previous value
C) Only if members are the same type
D) Only if compiler optimization is disabled
Answer: B
65. Can the same memory in a union be used to store an integer on one run and a pointer on another? What caution applies?
A) Yes, but beware pointer size/representation and alignment differences between types and platforms
B) No — pointers and integers cannot share memory ever
C) Yes — and it’s always fully portable without caution
D) Only if integers are larger than pointers
Answer: A
66. Is it legal to create an array of unions? Example: union U arr[10];
A) Yes — arrays of unions are legal and behave like arrays of other objects
B) No — unions cannot be array elements
C) Only if unions are typedef’d
D) Only if arrays are of size 1
Answer: A
67. Which of these is a difference between struct and union initializers?
A) For structs you can initialize members in order; for unions without designated initializer only the first member can be initialized implicitly
B) Unions support multi-member initialization like {1,2,3}
C) Struct initializers must use designators always
D) You cannot designate members in union initializers
Answer: A
68. Consider union { int i; struct { char c1; char c2; } s; } u; If you set u.s.c1 = ‘a’; then read u.i, this read is:
A) Implementation-defined (type punning)
B) Guaranteed to be ASCII code and portable
C) Always zero
D) A syntax error
Answer: A
69. What does gcc -fno-strict-aliasing influence relevant to unions?
A) It relaxes some optimization assumptions which may affect behavior of type-punning through pointers; unions are not directly changed but aliasing-related code may behave differently
B) It makes unions illegal
C) It changes the size of unions
D) It removes alignment requirements for unions
Answer: A
70. Which scenario would best justify the use of a union in a program?
A) You need to store different types in the same memory location depending on runtime tag (a discriminated union or tagged union pattern)
B) You need a thread-safe object automatically
C) You want to increase memory usage intentionally
D) You want to hide data from the linker
Answer: A
71. If two members of a union share identical type and size but have different names, reading the non-assigned one after assigning the other is:
A) Well-defined and equivalent to reading the assigned one (because bit-patterns are same)
B) Always undefined by the standard — cannot be read
C) Always returns zero
D) Causes linker errors
Answer: A
72. Which of these is true about pointers to members of unions?
A) You cannot form a pointer to a union member if the union is anonymous
B) You may take address of a member and form a pointer — pointer arithmetic must respect member’s type and alignment
C) Pointers to union members are of type void* only
D) Member pointers do not exist for unions
Answer: B
73. Suppose union U { int i; char c; } u; and u.i = INT_MAX; then reading u.c yields:
A) A platform-dependent byte (the low-order byte or other depending on endianness)
B) Always ‘\0’
C) Always 1
D) Always -1
Answer: A
74. Which of the following best describes “common initial sequence” concept?
A) When two structs share the same initial member types and order, code can access those members safely when used in unions in certain contexts
B) It means all members of a union share values in common
C) It is a concept that applies only to arrays and not unions
D) It mandates that structs must have identical sizes
Answer: A
75. Are bit-fields in unions treated differently from regular members regarding overlapping?
A) Bit-fields still occupy union storage and can overlap other members — their behavior follows union overlapping rules
B) Bit-fields in unions are automatically separated and do not overlap
C) Bit-fields cannot be used with unions
D) Bit-fields in unions are stored in a separate memory area from other members
Answer: A
76. Does memcpy between two unions of the same type preserve the active member?
A) It copies raw bytes; the active member concept remains consistent by copying the underlying pattern, but semantic active-member tracking is not part of bytes — practically the same bytes get copied
B) It always causes undefined behavior
C) memcpy cannot be used on unions
D) memcpy sets all members to uninitialized
Answer: A
77. In a union of pointers and integers, what is a portability concern?
A) Size and representation of pointer vs integer types may differ across platforms (pointer truncation)
B) Pointers are always larger than integers so no concern
C) The union will convert pointers to ASCII strings
D) The union will be automatically aligned to 1 byte making pointers invalid
Answer: A
78. If you embed a union as the final member of a struct, what is one use-case?
A) To implement a tagged union pattern where struct has a tag plus union variant (variant at end)
B) To guarantee the struct becomes immutable
C) To make the struct size always fixed at 1 byte
D) To allow the struct to be used as a function pointer
Answer: A
79. Which is true about initializer lists like union U u = { .x = 5 }; in C?
A) Designated initialization for unions is supported since C99 and sets the specified member as active
B) Designated initializers are only allowed for structs, not unions
C) Designated initializers are a C++ only feature
D) This syntax is always a compile-time error
Answer: A
80. When a union contains different pointer types, reading a pointer of one type after writing a different pointer type is:
A) Potentially subject to strict aliasing issues and pointer representation differences — caution advised
B) Always safe and portable
C) Disallowed by the compiler always
D) Requires volatile qualifier to work
Answer: A
81. In embedded systems, union usage is often favored for:
A) Memory-efficient overlay of hardware registers and different views of same memory-mapped region
B) Guaranteeing thread-safety in ISRs
C) Automatically optimizing peripherals for low power
D) Preventing bus contention
Answer: A
82. Which of the following code snippets defines a union and a typedef alias U?
A) typedef union { int x; float y; } U;
B) typedef struct union { int x; } U;
C) typedef union U { int x; }
D) union typedef U { int x; }
Answer: A
83. Which of these statements is correct about copying unions?
A) Assigning one union to another performs a shallow copy of bytes (copy of the active representation)
B) Unions cannot be assigned to each other
C) Assigning copies all members individually
D) Assigning unions always fails at runtime
Answer: A
84. If a union member is a struct with a constructor (C++), what special concern exists that doesn’t exist in C?
A) Constructor/destructor invocation rules — in C++ non-trivial types in unions require special handling; C has no constructors so this is not a concern
B) C++ prohibits unions with structs entirely
C) C will call constructors automatically but C++ won’t
D) This is identical in C and C++
Answer: A
85. Which header or language feature is required to use designated initializers like .x = 1?
A) Designated initializers are part of standard C99 and later — no special header required
B) You must include <designator.h>
C) They are enabled only with GCC extensions
D) They require C++11 only
Answer: A
86. Suppose we need to read raw bytes of a union safely portably. Which is the most portable method?
A) Use memcpy to copy union bytes into an array of unsigned char and inspect that — reading object representation via unsigned char is permitted
B) Cast the union pointer to char* and read directly — always portable in all cases
C) Use pointer cast to another incompatible type — portable and recommended
D) Rely on reading non-active members directly — always portable
Answer: A
87. Which of these statements regarding union member order is true?
A) Member order can affect the union initializer semantics if no designator is used (first member) and may affect source-code clarity, but sizeof depends on largest member, not order
B) Member order always changes sizeof by summation of sizes
C) Member order is irrelevant to initialization and always optimized away by compiler
D) Member order determines alignment of the union in ascending order only
Answer: A
88. Can a union contain bit-fields that are declared with different underlying types? Example int a:3; unsigned b:4; inside union.
A) Yes — bit-fields of different signedness or types may appear; they still share the union storage
B) No — all bit-fields in a union must be of same type
C) Bit-fields are not allowed in unions at all
D) Only unsigned bit-fields are allowed in unions
Answer: A
89. Which of the following is a correct way to define an object that is either an int or a pointer using a union and tag?
A) struct Variant { enum { INT, PTR } tag; union { int i; void *p; } u; };
B) union Variant { int tag; int i; void *p; };
C) struct Variant { int *u; };
D) union { int i; } Variant;
Answer: A
90. In terms of debugging, why might unions be harder to inspect in debuggers?
A) Because only one member is active and debuggers may show raw bytes or the last-assigned view, making interpretation ambiguous without program knowledge
B) Unions cannot be displayed by debuggers
C) Debuggers always crash on unions
D) Unions are always zeroed so debugger shows nothing
Answer: A
91. Which of these is true about using unions as overlays for hardware registers mapped at a fixed address?
A) It is a common practice in embedded systems but must be used carefully regarding volatile qualifiers and alignment
B) Unions cannot be used for memory-mapped I/O
C) Unions automatically add memory barriers needed for hardware access
D) Unions ensure atomic access to registers automatically
Answer: A
92. What is the effect of declaring a union variable as volatile?
A) It tells the compiler that accesses to the union (read/write) must not be optimized away and must be performed as written (useful for hardware or concurrency)
B) It makes the union read-only permanently
C) It changes the layout of the union in memory
D) It invalidates all pointers into union members
Answer: A
93. Which of the following is safest when using unions across different compilers/platforms?
A) Use unions only for alternatives whose binary layouts you control and document; avoid relying on byte-level aliasing across different types without explicit conversion via unsigned char or memcpy
B) Assume union layout is identical across all compilers and OSes
C) Use unions to share pointers between processes
D) Use unions to bypass memory safety of the language intentionally
Answer: A
94. Consider union { int i; float f; } u; — which cast is guaranteed to produce the same pointer value for &u?
A) (void*)&u and (char*)&u are both valid and point to the start of the union object
B) (int*)&u will always point to the int i member regardless of member order
C) Casting union address to any type is undefined unpredictably
D) Only (double*)&u is valid
Answer: A
95. Which is correct about using unions in function prototypes?
A) You can declare function parameters that are unions: void foo(union U u); is valid
B) Function prototypes cannot include unions
C) Unions as parameters must be passed by pointer only
D) Unions in prototypes must be extern only
Answer: A
96. Which of the following is a difference between struct and union with respect to initialization without designators?
A) For struct, elements are initialized in order; for union, the initializer without designator initializes the first member only
B) For union, initializer without designator initializes all members sequentially
C) For struct, initializer without designator is illegal
D) Structs and unions behave identically always for initialization
Answer: A
97. Suppose you need a 64-bit field that can be accessed as uint64_t or as two uint32_t parts. Which is the most appropriate C construct?
A) A union containing uint64_t u64; uint32_t parts[2]; (plus care with endianness)
B) A struct with both members (double storage)
C) Using #define macros only
D) Use two separate global variables with different names only
Answer: A
98. Which of these statements about unnamed/anonymous unions inside a struct is true (common compiler support)?
A) Anonymous union members allow direct access to union members via struct variable without naming the union — supported as extension or standard in some versions of C — use with caution for portability
B) Anonymous unions are illegal everywhere and cannot be used inside structs
C) Anonymous unions require runtime allocation
D) Anonymous unions convert struct into a pointer type
Answer: A
99. What is one reason unions can make code harder to maintain?
A) Because the active member must be tracked by programmer (usually via a tag), otherwise later readers might misinterpret raw bytes causing bugs
B) Unions are always bigger than equivalent structs, making maintenance harder
C) Unions auto-generate code that is unreadable
D) Unions cannot be named and are anonymous by definition
Answer: A
100. Which of these is an appropriate guideline when designing a tagged union in C?
A) Always include an explicit tag (enum) in the containing struct to indicate which union member is active; access only the indicated member
B) Never use enums for tags — use magic numbers in comments
C) Omit any indication — let callers guess active member
D) Use unions without tags to permit arbitrary access in any order
Answer: A
