Showing 41–50 of 100 questions
in Structures & Unions
How do you access a member of a structure through a pointer in C?
A
Using dot operator (.)
B
Using arrow operator (->)
C
Using asterisk (*)
D
Using ampersand (&)
Correct Answer:
B. Using arrow operator (->)
EXPLANATION
The arrow operator (->) is used to access structure members through a pointer. The dot operator is used with direct variables.
Which feature of structures makes them suitable for implementing linked lists?
A
Self-referential pointers
B
Dynamic memory allocation
C
Array of structures
D
Structure padding
Correct Answer:
A. Self-referential pointers
EXPLANATION
Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.
Consider: union Data { int i; float f; char c; }; What is the size of this union?
A
4 bytes
B
9 bytes
C
5 bytes
D
12 bytes
Correct Answer:
A. 4 bytes
EXPLANATION
The size of a union equals the size of its largest member. Here, int is 4 bytes (largest), so union size is 4 bytes.
Which of the following statements about unions is correct?
A
Union members share the same memory location
B
Union size equals the sum of all member sizes
C
All union members can hold values simultaneously
D
Union requires more memory than structure
Correct Answer:
A. Union members share the same memory location
EXPLANATION
Union members share the same memory location, and the union size equals the size of its largest member.
What is the size of the following structure on a 32-bit system?
struct Point { char c; int x; };
A
5 bytes
B
8 bytes
C
9 bytes
D
6 bytes
Correct Answer:
B. 8 bytes
EXPLANATION
Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.
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
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.
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.
What is the correct way to dynamically allocate an array of structures?
struct Student { int roll; char name[50]; };
A
struct Student *arr = (struct Student*)malloc(n * sizeof(struct Student));
B
struct Student arr = (struct Student*)malloc(n * sizeof(char));
C
struct Student *arr = malloc(n);
D
Student *arr = malloc(n * Student);
Correct Answer:
A. struct Student *arr = (struct Student*)malloc(n * sizeof(struct Student));
EXPLANATION
Correct allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).
When structure is passed by value to a function, which of these is true?
A
Changes to structure members inside function affect the original
B
The entire structure is copied onto the stack
C
Only pointers within the structure are copied
D
Memory is shared between function and caller
Correct Answer:
B. The entire structure is copied onto the stack
EXPLANATION
Pass by value creates a complete copy of the structure on the function's stack frame.
What is the primary use case for unions in embedded systems?
A
Efficient type casting and hardware register mapping
B
Storing multiple values simultaneously
C
Organizing large data structures
D
Improving code readability
Correct Answer:
A. Efficient type casting and hardware register mapping
EXPLANATION
Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.