Consider union u { int i; char c; float f; }; What is the size of this union?
A 4 bytes B 9 bytes C 7 bytes D Depends on compiler
Union size equals the largest member size. float is typically 4 bytes, which is largest.
What will be the output of this code? union Data { int x; char y; }; Data d = {65}; d.y = 'A'; printf("%d", d.x);
A 65 B 97 C 65 then 97 D Undefined behavior
Modifying y changes the same memory location as x. Only the last byte of x is affected, leading to unpredictable output.
Which of these is a valid declaration of a structure variable using dot operator immediately after struct definition?
A struct Point { int x; int y; } p; B struct Point { int x; int y; }; Point p; C struct { int x; int y; } p; D Both A and C
Both methods are valid - A uses named struct tag with variable declaration, C uses anonymous struct with variable declaration.
What is the issue with bit fields in structures regarding portability?
A Bit field ordering is implementation-dependent B Bit fields cannot be used with pointers C Bit fields always consume 32 bits regardless of size D Bit fields cannot be initialized
Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.
In a structure with flexible array members, which statement is INCORRECT? struct FlexArray { int len; int arr[]; };
A Flexible array member must be the last member B Structure can be allocated on stack directly C Memory for array must be allocated separately D It is a C99 standard feature
Flexible array members require dynamic allocation; the structure itself cannot be allocated on the stack with a defined size for the array.
What happens when you access a union member that wasn't the last one to be written?
A It returns the last written value B It returns the correctly stored value for that member C It returns garbage or bitwise interpretation of current memory D Compilation error occurs
All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.
Which of the following correctly demonstrates self-referential structure (for linked list)?
A struct Node { int data; struct Node *next; }; B struct Node { int data; Node *next; }; C struct Node { int data; *next; }; D Node { int data; Node *next; };
In C, self-referential structures require the struct keyword in pointer declarations within the structure definition.
What is the output of the following? struct S { int a:3; int b:3; int c:3; }; printf("%zu", sizeof(struct S));
A 1 B 4 C 12 D Implementation dependent
Bit field packing is implementation-defined. Size depends on compiler's bit field allocation strategy.
When comparing nested structures, which access method is INVALID? struct Address { char city[20]; }; struct Person { Address addr; }; Person p;
A p.addr.city B (&p)->addr.city C p->addr.city D Both B and C valid
p is a structure variable (not pointer), so arrow operator (->) cannot be used. Only dot operator works.
Which structure feature is most suitable for implementing a state machine with limited states?
A Bit fields B Union with enum C Nested structures D Flexible arrays
Bit fields efficiently store boolean/limited-range states using minimal memory, ideal for state machines.
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
Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.
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
Pass by value creates a complete copy of the structure on the function's stack frame.
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 allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).
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
Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.
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
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.
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
Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.
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
Union members share the same memory location, and the union size equals the size of its largest member.
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
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 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
Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.
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 (&)
The arrow operator (->) is used to access structure members through a pointer. The dot operator is used with direct variables.