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.6Easy
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.7Easy
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.8Easy
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.
Q.9Easy
Which keyword is used to define a new name for a structure in C?
Answer: B
The typedef keyword creates an alias for a data type, allowing you to use a shorter name instead of the full struct declaration.
Q.10Easy
What is the purpose of padding in C structures?
Answer: B
Padding adds unused bytes between members to align them on word boundaries, improving CPU access efficiency and performance.
Q.11Easy
How do you access a structure member through a pointer in C?
Answer: B
The arrow operator (->) is used to access structure members through a pointer. It dereferences the pointer and accesses the member in one operation.
Q.12Easy
What is the primary advantage of using structures in C?
Answer: B
Structures allow you to combine multiple data types into a single composite type, making code organization and data management more efficient.
Q.13Easy
What is the output of the following code?
struct Point { int x; int y; };
struct Point p = {5};
printf("%d %d", p.x, p.y);
Answer: A
When initializing a struct with fewer values than members, remaining members are zero-initialized. p.x=5, p.y=0.
Q.14Easy
Which of the following is true about typedef struct?
Answer: A
typedef creates an alias for the struct type, allowing direct use without 'struct' keyword.
Q.15Easy
Consider union u { int i; char c; float f; }; What is the size of this union?
Answer: A
Union size equals the largest member size. float is typically 4 bytes, which is largest.
Q.16Easy
Which of these is a valid declaration of a structure variable using dot operator immediately after struct definition?
Answer: D
Both methods are valid - A uses named struct tag with variable declaration, C uses anonymous struct with variable declaration.
Q.17Easy
Which of the following correctly demonstrates self-referential structure (for linked list)?
Answer: A
In C, self-referential structures require the struct keyword in pointer declarations within the structure definition.
Q.18Easy
When structure is passed by value to a function, which of these is true?
Answer: B
Pass by value creates a complete copy of the structure on the function's stack frame.
Q.19Easy
What is the correct way to dynamically allocate an array of structures?
struct Student { int roll; char name[50]; };
Answer: A
Correct allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).
Q.20Easy
Which of the following statements about unions is correct?
Answer: A
Union members share the same memory location, and the union size equals the size of its largest member.