In competitive programming for GATE/ISRO 2025, which scenario would union be preferable over structure?
AWhen you need to store multiple data types independently
BWhen memory optimization is critical and you need mutually exclusive data
CWhen creating complex data structures like trees
DWhen implementing object-oriented features
Correct Answer:
B. When memory optimization is critical and you need mutually exclusive data
EXPLANATION
Unions are preferred when you have mutually exclusive data (only one member is used at a time) and memory is critical. A classic example is storing different data types in a tagged union pattern used in compilers and interpreters.
Given a structure with array members, how would you efficiently pass it to a function to avoid copying overhead?
APass by value directly
BPass a pointer to the structure
CUse typedef with the structure
DConvert to union before passing
Correct Answer:
B. Pass a pointer to the structure
EXPLANATION
Passing a pointer to the structure avoids copying the entire structure in memory, which is especially important for large structures with array members. This improves performance significantly.
Which of the following statements about structure initialization in C is INCORRECT?
ADesignated initializers allow setting members in any order
BAll members must be explicitly initialized during declaration
CMembers can be initialized during structure definition
DPartial initialization results in uninitialized members having indeterminate values
Correct Answer:
B. All members must be explicitly initialized during declaration
EXPLANATION
Members do not need to be explicitly initialized. Uninitialized members in automatic structures have indeterminate values, while static structures have zero-initialized members. Designated initializers (C99+) allow flexible initialization order.
struct Node { int data; struct Node *next; }; This is an example of which design pattern?
ARecursive structure
BSelf-referential structure
CCircular structure
DPolymorphic structure
Correct Answer:
B. Self-referential structure
EXPLANATION
A self-referential structure contains a pointer to its own type. This is the fundamental building block for creating linked lists, trees, and other dynamic data structures.
In the context of bit fields in structures, what is the maximum number of bits that can be allocated to a single member in standard C?
A8 bits
B16 bits
CSize of the underlying type in bits
D32 bits
Correct Answer:
C. Size of the underlying type in bits
EXPLANATION
The maximum number of bits for a bit field member cannot exceed the size of the underlying type. For int (typically 32 bits), you can allocate up to 32 bits to a single member.
What will be the output of the following code?
union Test {
int a;
char b;
};
union Test t;
t.a = 65;
printf("%d %c", t.a, t.b);
A65 A
B65 65
C0 A
DDepends on endianness
Correct Answer:
D. Depends on endianness
EXPLANATION
Since union members share memory, the output depends on the system's endianness. On little-endian systems, t.b would be 65 (or 'A'), but on big-endian systems, the result differs.
Which feature in C allows you to define a structure without a tag name for use within another structure?
ATagged union
BAnonymous structure
CTypedef structure
DNested enumeration
Correct Answer:
B. Anonymous structure
EXPLANATION
Anonymous structures allow defining structures without a tag name directly inside another structure, making members accessible as if they belong to the outer structure.
Consider the following code:
struct Data {
int x;
char y;
double z;
};
Assuming int=4 bytes, char=1 byte, double=8 bytes, what is the size of struct Data due to padding?
A13 bytes
B16 bytes
C20 bytes
D24 bytes
Correct Answer:
D. 24 bytes
EXPLANATION
Memory alignment causes padding. x(4) + padding(4) + y(1) + padding(7) + z(8) = 24 bytes. The compiler aligns to the largest member (double = 8 bytes).
What is the key difference between a structure and a union in C?
AUnion allocates memory for all members simultaneously, structure allocates for each member separately
BStructure allocates memory equal to sum of all members, union allocates memory equal to the largest member
CUnion can have function pointers, structure cannot
DStructure supports inheritance, union does not
Correct Answer:
B. Structure allocates memory equal to sum of all members, union allocates memory equal to the largest member
EXPLANATION
In structures, memory is allocated for each member independently. In unions, all members share the same memory location, so total memory allocated equals the size of the largest member.
In competitive programming, when you need a structure that can hold either an integer or a floating-point number (but not both simultaneously) in the most memory-efficient way, which should you use?
Astruct with both members
Bunion with both members
CAn array of size 2
DA single variable with type casting
Correct Answer:
B. union with both members
EXPLANATION
Union is memory-efficient as members share space. Only one can be accessed at a time, which suits the requirement.