What happens when you access a union member that wasn't the last one to be written?
Answer: C
All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.
Q.22Medium
When comparing nested structures, which access method is INVALID?
struct Address { char city[20]; };
struct Person { Address addr; };
Person p;
Answer: C
p is a structure variable (not pointer), so arrow operator (->) cannot be used. Only dot operator works.
Q.23Medium
Which structure feature is most suitable for implementing a state machine with limited states?
Answer: A
Bit fields efficiently store boolean/limited-range states using minimal memory, ideal for state machines.
Q.24Medium
What is the primary use case for unions in embedded systems?
Answer: A
Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.
Q.25Medium
What is the size of the following structure on a 32-bit system?
struct Point { char c; int x; };
Answer: B
Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.
Advertisement
Q.26Medium
Which feature of structures makes them suitable for implementing linked lists?
Answer: A
Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.
Q.27Medium
What happens if you attempt to assign one structure variable to another of the same type?
Answer: B
In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).
Q.28Medium
Which of the following is a valid declaration of nested structure?
Answer: C
Both nested structure definitions (defining B inside A and declaring B as member) are valid in C.
Q.29Medium
What is the primary advantage of using unions in embedded systems?
Answer: A
Unions are primarily used in embedded systems to conserve memory by allowing multiple members to share the same memory location.
Q.30Medium
How can you create a structure that can hold either an integer or a floating-point number, but not both simultaneously?
Answer: B
A union is the appropriate choice when you need to store different data types but only one value at a time.
Q.31Medium
What is the difference between 'struct' and 'typedef struct' in C?
Answer: B
typedef struct creates a type alias, allowing you to declare variables without using the 'struct' keyword each time.
Q.32Medium
What will be the output if you read a union member that was not last written to?
Answer: A
Reading a union member that wasn't last written gives unpredictable results (garbage value or leftover bits from previous member).
Q.33Medium
In a nested structure scenario, how do you access a member of the inner structure?
Answer: C
Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.
Q.34Medium
What is the size of the following structure on a 32-bit system?
struct Point {
char c;
int x;
double y;
}
Answer: D
char(1) + padding(3) + int(4) + double(8) = 16 bytes minimum, but alignment to double boundary makes it 24 bytes.
Q.35Medium
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);
Answer: C
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.
Q.36Medium
What is the size of the following union?
union Test {
int a[5];
char b[10];
double c;
}
Answer: B
Union size equals the largest member. int[5] = 20 bytes, char[10] = 10 bytes, double = 8 bytes. Maximum is 20 bytes.
Q.37Medium
Consider a structure with bit fields:
struct Flags {
unsigned int flag1 : 1;
unsigned int flag2 : 3;
unsigned int flag3 : 4;
};
What is the minimum size of this structure?
Answer: C
Bit fields must fit within an underlying integer type. Total bits = 8, which requires at least 4 bytes (one int on most systems).
Q.38Medium
What will happen if you try to compare two structures using the == operator directly?
Answer: B
C does not support direct comparison of structures using ==. You must compare members individually or use memcmp().
Q.39Medium
Which of the following is a valid way to pass a structure to a function?
Answer: C
Structures can be passed by value (copying the entire structure) or by pointer (passing address) for efficiency.
Q.40Medium
In a nested structure scenario like struct A contains struct B, how do you access a member of B from A's variable?
Answer: C
If B is embedded in A: a_var.b_var.member. If B is a pointer in A: a_var->b_var->member or a_var.b_ptr->member.