Showing 31–40 of 100 questions
in Structures & Unions
What will be the output if you read a union member that was not last written to?
A
Garbage value or previous data
B
Zero by default
C
Compiler warning only
D
Runtime error
Correct Answer:
A. Garbage value or previous data
EXPLANATION
Reading a union member that wasn't last written gives unpredictable results (garbage value or leftover bits from previous member).
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.
What is the difference between 'struct' and 'typedef struct' in C?
A
No difference, both are identical
B
typedef struct creates an alias, avoiding the need to write 'struct' keyword repeatedly
C
struct is faster than typedef struct
D
typedef struct only works with unions
Correct Answer:
B. typedef struct creates an alias, avoiding the need to write 'struct' keyword repeatedly
EXPLANATION
typedef struct creates a type alias, allowing you to declare variables without using the 'struct' keyword each time.
How can you create a structure that can hold either an integer or a floating-point number, but not both simultaneously?
A
Using a structure with conditional compilation
B
Using a union instead of a structure
C
Using an array of mixed types
D
Using a pointer to void
Correct Answer:
B. Using a union instead of a structure
EXPLANATION
A union is the appropriate choice when you need to store different data types but only one value at a time.
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.
Which of the following correctly demonstrates a typedef for a structure?
A
typedef struct { int x; int y; } Point;
B
struct typedef Point { int x; int y; };
C
define struct Point { int x; int y; };
D
struct Point typedef { int x; int y; };
Correct Answer:
A. typedef struct { int x; int y; } Point;
EXPLANATION
The correct syntax for typedef struct is: typedef struct { members } StructName;
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.
What is the primary advantage of using unions in embedded systems?
A
Reduced memory consumption
B
Faster execution
C
Better readability
D
Easier debugging
Correct Answer:
A. Reduced memory consumption
EXPLANATION
Unions are primarily used in embedded systems to conserve memory by allowing multiple members to share the same memory location.
Which of the following is a valid declaration of nested structure?
A
struct A { int x; struct B { int y; } b; };
B
struct A { int x; struct B b; };
C
Both A and B are valid
D
Neither A nor B is valid
Correct Answer:
C. Both A and B are valid
EXPLANATION
Both nested structure definitions (defining B inside A and declaring B as member) are valid in C.
What happens if you attempt to assign one structure variable to another of the same type?
A
Compilation error occurs
B
All members are copied
C
Only pointers are copied
D
Runtime error occurs
Correct Answer:
B. All members are copied
EXPLANATION
In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).