What is the primary difference between a structure and a union in C?
Answer: A
In structures, each member gets its own memory space. In unions, all members share the same memory location, so only one member can hold a value at a time.
Q.2Easy
What will be the output of sizeof(union test) if union has members: int a, char b, float c?
Answer: A
Union size equals the size of its largest member. Here, both int and float are 4 bytes (largest), so sizeof(union test) = 4 bytes.
Q.3Easy
Which keyword is used to define a structure in C?
Answer: B
The 'struct' keyword is used to define structures in C. 'class' is used in C++ and Java.
Q.4Easy
What is the size of the following structure?
struct point { int x; int y; float z; };
Answer: B
int x = 4 bytes, int y = 4 bytes, float z = 4 bytes. Total = 12 bytes (no padding in this case on most systems).
Q.5Easy
Can we pass a structure variable to a function in C?
Answer: C
Structures can be passed by value (copy of structure) or by reference (using pointers to structure).
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.9Medium
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.10Medium
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.
Q.11Medium
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.12Medium
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.13Hard
Which statement is true about nested structures in C?
Answer: B
C allows structures to have other structures as members. This is called nested structures and is a common practice.
Q.14Hard
What happens if you assign a union member and then access another member?
struct u { int a; char b; }; u.a = 257; printf("%d", u.b);
Answer: B
In a union, both members share memory. 257 in binary is 100000001. u.b (char) reads only the LSB, which is 1.
Q.15Hard
How can you initialize a structure array of 10 elements partially in C?
Answer: D
Both {0} and {} will initialize all members to zero. These are equivalent in C99 and later standards.
Q.16Hard
What is the difference between self-referential and recursive structures?
Answer: B
Self-referential structures contain pointers to the same structure type (like linked list nodes). Recursion refers to function calls, not structures.
Q.17Hard
Given union test { int x; char y[4]; }; If you set y[0]=65, y[1]=66, what happens to x?
Answer: C
Since union members share memory, setting y[0]=65 and y[1]=66 overwrites the int x value. The exact value depends on endianness.
Q.18Easy
Which of the following is a key difference between struct and union in C?
Answer: B
In unions, all members share the same memory location, so only one member can hold a value at a time. In structs, each member has its own memory space.
Q.19Easy
What will be the output of sizeof() for the following union? union data { int a; float b; double c; }
Answer: B
The size of a union is the size of its largest member. Here, double (8 bytes) is the largest, so sizeof(union data) = 8 bytes.
Q.20Easy
In C, what is a self-referential structure?
Answer: A
A self-referential structure contains a pointer to its own type, commonly used in creating linked lists, trees, and other data structures.