C Programming — Structures & Unions
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 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
Given a structure with array members, how would you efficiently pass it to a function to avoid copying overhead?
A Pass by value directly
B Pass a pointer to the structure
C Use typedef with the structure
D Convert 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.

Take Test
Which of the following statements about structure initialization in C is INCORRECT?
A Designated initializers allow setting members in any order
B All members must be explicitly initialized during declaration
C Members can be initialized during structure definition
D Partial 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.

Take Test
struct Node { int data; struct Node *next; }; This is an example of which design pattern?
A Recursive structure
B Self-referential structure
C Circular structure
D Polymorphic 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.

Take Test
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?
A 8 bits
B 16 bits
C Size of the underlying type in bits
D 32 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.

Take Test
Advertisement
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
Which feature in C allows you to define a structure without a tag name for use within another structure?
A Tagged union
B Anonymous structure
C Typedef structure
D Nested 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.

Take Test
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?
A 13 bytes
B 16 bytes
C 20 bytes
D 24 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).

Take Test
What is the key difference between a structure and a union in C?
A Union allocates memory for all members simultaneously, structure allocates for each member separately
B Structure allocates memory equal to sum of all members, union allocates memory equal to the largest member
C Union can have function pointers, structure cannot
D Structure 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.

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
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