Showing 21–30 of 100 questions
in Structures & Unions
What is the size of the following union?
union Test {
int a[5];
char b[10];
double c;
}
A
10 bytes
B
20 bytes
C
30 bytes
D
24 bytes
Correct Answer:
B. 20 bytes
EXPLANATION
Union size equals the largest member. int[5] = 20 bytes, char[10] = 10 bytes, double = 8 bytes. Maximum is 20 bytes.
Which of the following correctly demonstrates accessing a member of a pointer to a structure?
A
ptr.member
B
(*ptr).member
C
**ptr.member
D
ptr->member->member
Correct Answer:
B. (*ptr).member
EXPLANATION
Both (*ptr).member and ptr->member are valid. Option B uses explicit dereferencing, while the arrow operator is syntactic sugar for this.
What will be the output of this code?
union Data {
int a;
char b;
};
union Data d;
d.a = 257;
printf("%d %c", d.a, d.b);
A
257 A
B
257 \0
C
1 A
D
257 1
EXPLANATION
Union shares memory. 257 in int format overwrites the char. On a little-endian system, only the lower byte (1) is stored in the char portion.
What is the primary difference between a struct and a union in C?
A
Struct members are stored sequentially; union members share the same memory location
B
Union members are stored sequentially; struct members share memory
C
Both allocate separate memory for each member
D
There is no functional difference
Correct Answer:
A. Struct members are stored sequentially; union members share the same memory location
EXPLANATION
In a union, all members occupy the same memory space, while struct members have individual memory locations.
Consider the following code:
struct Data {
int a;
char b;
};
struct Data d = {10, 'A'};
Which method of initialization is used here?
A
Designated initialization
B
Positional initialization
C
Named initialization
D
Array initialization
Correct Answer:
B. Positional initialization
EXPLANATION
Values are assigned in the order they appear in the structure definition, which is positional initialization.
What is the size of the following structure on a 32-bit system?
struct Point {
char c;
int x;
double y;
}
A
13 bytes
B
16 bytes
C
20 bytes
D
24 bytes
Correct Answer:
D. 24 bytes
EXPLANATION
char(1) + padding(3) + int(4) + double(8) = 16 bytes minimum, but alignment to double boundary makes it 24 bytes.
Which of the following statements about structures is TRUE?
A
A structure is a collection of variables of the same data type
B
A structure is a collection of variables of different data types grouped under a single name
C
A structure cannot contain pointers
D
A structure occupies memory equal to the sum of its members without any padding
Correct Answer:
B. A structure is a collection of variables of different data types grouped under a single name
EXPLANATION
A structure allows grouping of different data types. Padding may be added by the compiler for alignment.
In a nested structure scenario, how do you access a member of the inner structure?
A
outer.inner.member
B
outer->inner->member
C
Both A and B (depending on whether it's direct or pointer)
D
outer[inner][member]
Correct Answer:
C. Both A and B (depending on whether it's direct or pointer)
EXPLANATION
Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.
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.
Which of the following is the correct way to initialize a structure in C?
A
struct Point p = {10, 20};
B
struct Point p; p = {10, 20};
C
Point p = {10, 20};
D
p.x = 10; p.y = 20; (without declaration)
Correct Answer:
A. struct Point p = {10, 20};
EXPLANATION
Structures must be initialized at declaration time using curly braces. Assignment of initializer list is not valid after declaration.