What happens when you initialize a structure variable without assigning values?
Answer: B
Uninitialized local structure variables contain garbage values. Global structures are automatically initialized to zero.
Q.2Medium
Which of the following correctly declares a pointer to a structure?
Answer: A
Correct syntax is 'struct typename *pointerName;'. Option A follows proper declaration syntax.
Q.3Medium
What is the difference between struct and typedef struct?
Answer: B
typedef struct creates an alias, so you can declare variables directly using the alias name without repeating 'struct'.
Q.4Medium
What will be printed?
struct s { int a; char b; int c; }; printf("%lu", sizeof(struct s));
Answer: B
Due to memory alignment/padding: int a(4) + char b(1) + 3 padding bytes + int c(4) = 12 bytes.
Q.5Medium
How do you access a member of a structure using a pointer?
Answer: B
The arrow operator (->) is used to access structure members through a pointer. Dot (.) is for direct variables.
Advertisement
Q.6Medium
What is the purpose of padding in structures?
Answer: B
Padding ensures that structure members are aligned in memory according to their natural boundaries, improving CPU access speed.
Q.7Medium
Consider: union u { int x; double y; }; What is sizeof(u)?
Answer: B
The size of a union is equal to its largest member. double is typically 8 bytes, which is larger than int (4 bytes).
Q.8Medium
Consider struct test { int x; double y; char z; }; What is the minimum size of this structure (assuming 4-byte int, 8-byte double)?
Answer: D
Due to padding: int (4) + padding (4) + double (8) + char (1) + padding (3) = 24 bytes. The structure aligns to the largest member size.
Q.9Medium
What will be printed by this code? struct s { int a; struct s *next; }; struct s *ptr; sizeof(struct s)
Answer: C
sizeof(int) = 4 bytes, sizeof(pointer) = 8 bytes (on 64-bit systems). Total = 12 bytes. Pointers are fixed size regardless of what they point to.
Q.10Medium
In a nested structure, how do you access the innermost member? struct outer { struct inner { int value; } in; } obj;
Answer: B
For nested structures accessed through an object, use the dot operator multiple times: obj.in.value accesses the value member of the inner structure.
Q.11Medium
Which of the following correctly initializes a structure array? struct point { int x, y; } arr[3] = ?
Answer: A
Structure arrays are initialized with nested braces, where each set of braces contains initialization values for one structure element.
Q.12Medium
What happens when you modify a union member that overlaps with another?
Answer: C
Since union members share memory, modifying one member overwrites the value of other members because they occupy the same memory location.
Q.13Medium
Which statement is correct about bit fields in structures?
Answer: B
Bit fields allow you to specify the number of bits for integral members, enabling memory optimization by packing multiple small values into a single byte.
Q.14Medium
What is the difference between struct tag and struct type in C?
Answer: B
In 'struct person { int age; } p;', 'person' is the tag (structure name) and 'p' is the type/variable. Tag names the structure template; type creates actual instances.
Q.15Medium
What will be the behavior of this code? struct s { int a; }; struct s obj = {5}; struct s *ptr = &obj; ptr->a = 10; printf("%d", obj.a);
Answer: B
ptr points to obj, so ptr->a = 10 modifies obj.a to 10. The printf outputs the modified value 10.
Q.16Medium
Which initialization method for structures is used in designated initializers (C99 feature)?
Answer: B
Designated initializers (C99) allow specifying members by name using dot notation, making code more readable and maintainable.
Q.17Medium
How much memory (in bytes) will the following structure occupy on a 32-bit system?
struct Data { char c; int i; short s; };
Answer: C
Due to padding/alignment: char(1) + padding(3) + int(4) + short(2) = 12 bytes on 32-bit systems.
Q.18Medium
What is the primary difference between passing a structure by value vs by pointer in function calls?
Answer: C
Passing by pointer avoids copying the entire structure, making it efficient for large data structures.
Q.19Medium
What will be the output of this code?
union Data { int x; char y; };
Data d = {65};
d.y = 'A';
printf("%d", d.x);
Answer: D
Modifying y changes the same memory location as x. Only the last byte of x is affected, leading to unpredictable output.
Q.20Medium
What is the issue with bit fields in structures regarding portability?
Answer: A
Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.