C Programming — Structures & Unions
C language from basics to advanced placement prep
20 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 20 questions in Structures & Unions
In competitive programming for GATE/ISRO 2025, which scenario would union be preferable over structure?
A When you need to store multiple data types independently
B When memory optimization is critical and you need mutually exclusive data
C When creating complex data structures like trees
D When 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.

Take Test
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);
A 65 A
B 65 65
C 0 A
D Depends 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.

Take Test
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?
A struct with both members
B union with both members
C An array of size 2
D A 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.

Take Test
What is the output of this complex code?
struct S { char c; int i; } s;
printf("%lu", sizeof(s));
A 5
B 8
C 6
D 4
Correct Answer:  B. 8
EXPLANATION

char(1 byte) + padding(3 bytes) + int(4 bytes) = 8 bytes due to alignment requirements.

Take Test
If you have a structure with bit fields, which statement about their behavior is true?
A Bit fields can improve memory efficiency for small values
B Bit fields guarantee portability across all platforms
C Bit fields allow taking addresses (&) of members
D Bit fields support all data types
Correct Answer:  A. Bit fields can improve memory efficiency for small values
EXPLANATION

Bit fields save memory by storing multiple values in single bytes, but behavior varies by compiler/platform, and you cannot take addresses of bit field members.

Take Test
Advertisement
In competitive programming, when implementing a graph using adjacency lists, which data structure is most suitable?
A Array of unions
B Array of structures containing linked lists
C Single structure
D Union of arrays
Correct Answer:  B. Array of structures containing linked lists
EXPLANATION

An array of structures, where each structure contains a linked list (using self-referential pointers), is ideal for adjacency list representation.

Take Test
If a structure contains a flexible array member, where must it be declared?
A At the beginning of the structure
B At the end of the structure
C Anywhere in the structure
D In a separate union
Correct Answer:  B. At the end of the structure
EXPLANATION

A flexible array member must be the last member of a structure. This is a C99 feature used for dynamic-sized arrays.

Take Test
In a structure, what does the #pragma pack(1) directive do?
A Removes padding between members
B Adds padding between members
C Compresses structure data
D Aligns structure to word boundary
Correct Answer:  A. Removes padding between members
EXPLANATION

#pragma pack(1) removes padding, making members occupy consecutive memory locations for compact storage.

Take Test
What will be the output of this code?
struct X { int a; float b; };
struct Y { struct X x; int c; };
printf("%zu", sizeof(struct Y));
A 12
B 13
C 16
D Cannot determine
Correct Answer:  C. 16
EXPLANATION

Nested struct X: int(4) + float(4) = 8 bytes. Y: struct X(8) + padding(0) + int(4) = 12, but with overall alignment it becomes 16 bytes.

Take Test
In competitive programming, when should you use union instead of struct?
A When you need to store multiple values simultaneously
B When memory optimization and type reinterpretation are critical
C When you have more than 5 members
D Never - struct is always better
Correct Answer:  B. When memory optimization and type reinterpretation are critical
EXPLANATION

Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips