structure in c language: 36 MCQs with answers
All 36 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 21 questions
Q1. What is the primary purpose of a structure in C?
BeginnerShow answer & explanation
To group variables of different types under a single name
Structures allow grouping of different data types into a single user-defined type, enabling organized data management.
Q2. Which keyword is used to define a structure?
BeginnerShow answer & explanation
`struct`
The `struct` keyword is used to define a structure, with members declared inside curly braces.
Q3. When is memory allocated for a structure?
BeginnerShow answer & explanation
When a structure variable is declared
A structure definition is just a blueprint; memory is allocated only when a variable of that type is declared.
Q4. Which operator is used to access a member of a structure variable?
BeginnerShow answer & explanation
`.`
The dot operator (`.`) is used to access members of a structure variable.
Q5. What does `ptr->member` mean in terms of dereferencing?
BeginnerShow answer & explanation
`(*ptr).member`
The arrow operator is a shorthand for dereferencing the pointer and then using the dot operator: `ptr->member` is equivalent to `(*ptr).member`.
Q6. What is the effect of `typedef struct Student Student;`?
BeginnerShow answer & explanation
It creates an alias `Student` for `struct Student`
`typedef` creates an alias, so you can write `Student` instead of `struct Student`.
Q7. Why might the size of a structure be larger than the sum of its members' sizes?
BeginnerShow answer & explanation
Because of padding for memory alignment
The compiler may insert padding bytes between members to align them to memory boundaries, increasing the total size.
Q8. Which of the following is a valid way to pass a structure to a function without copying the entire structure?
BeginnerShow answer & explanation
Pass a pointer to the structure
Passing a pointer avoids copying the whole structure and allows modification of the original.
Q9. What happens when you assign one structure variable to another (e.g., `s2 = s1;`)?
BeginnerShow answer & explanation
It copies all members from s1 to s2
Structure assignment is allowed and copies all members from the source to the destination.
Q10. Structures can be compared directly with the `==` operator in C.
BeginnerShow answer & explanation
False
C does not support direct comparison of structures with `==`; you must compare member by member.
Q11. The arrow operator (`->`) is equivalent to dereferencing a pointer and then using the dot operator.
BeginnerShow answer & explanation
True
`ptr->member` is exactly equivalent to `(*ptr).member`.
Q12. What is a structure in C?
BeginnerShow answer & explanation
—
It allows bundling related data together.
Q13. Does defining a structure allocate memory? Explain.
BeginnerShow answer & explanation
—
The definition is a blueprint.
Q14. How do you access a member of a structure variable?
BeginnerShow answer & explanation
—
The dot operator connects the variable and member.
Q15. How do you access a member through a pointer to a structure?
BeginnerShow answer & explanation
—
The arrow operator dereferences the pointer and accesses the member.
Q16. How can you create an alias for a structure?
BeginnerShow answer & explanation
—
This allows using `Student` instead of `struct Student`.
Q17. Is structure assignment allowed in C? What does it do?
BeginnerShow answer & explanation
—
For example, `s2 = s1;` copies all members.
Q18. How do you initialize a structure variable at declaration? Give an example.
BeginnerShow answer & explanation
—
Values must match the member types and order.
Q19. What is the difference between the dot operator and the arrow operator?
BeginnerShow answer & explanation
—
If you have a pointer, use `->`; if you have a variable, use `.`.
Q20. Can a structure contain an array as a member?
BeginnerShow answer & explanation
—
This makes structures versatile for modeling complex data.
Q21. How do you access a member of an array of structures?
BeginnerShow answer & explanation
—
First select the element, then the member.
Building up · 10 questions
Q22. What is the equivalence of `ptr->member`?
IntermediateShow answer & explanation
—
The arrow operator is a shorthand for dereferencing and dot access.
Q23. Can structures be compared directly with `==`? Why or why not?
IntermediateShow answer & explanation
—
The compiler does not know how to compare arbitrary structures.
Q24. Why might the size of a structure be larger than the sum of its members' sizes?
IntermediateShow answer & explanation
—
Alignment improves access speed but increases size.
Q25. What is the advantage of passing a structure by pointer to a function?
IntermediateShow answer & explanation
—
Passing by pointer is pass-by-reference.
Q26. What is the foundation for implementing linked lists in C?
IntermediateShow answer & explanation
—
Each node holds data and a pointer to the next node.
Q27. Given the following structure definition and code snippet, what will be the output? ```c struct Point { int x; int y; }; int main() { struct Point p1 = {10, 20}; struct Point p2; p2 = p1; p2.x = 30; printf("%d %d\n", p1.x, p2.x); return 0; } ```
IntermediateShow answer & explanation
10 30
In C, structure assignment `p2 = p1` copies the entire structure member-by-member, so `p2` gets a copy of `p1`'s values. Modifying `p2.x` does not affect `p1.x`. Therefore, `p1.x` remains 10 and `p2.x` becomes 30.
Q28. Which of the following correctly initializes a structure variable at declaration and then accesses a member using a pointer? ```c struct Student { char name[50]; int roll; }; ```
IntermediateShow answer & explanation
`struct Student s = {"Alice", 101}; struct Student *ptr = &s; printf("%s", ptr->name);`
To access a member through a pointer to a structure, you must use the arrow operator `->`. The correct syntax is `ptr->name`. Option A does exactly that. Other options either use incorrect pointer assignment (`ptr = s` instead of `&s`) or incorrect member access syntax (`ptr.name` or `*ptr.name`).
Q29. Consider the following structure: ```c struct Data { char c; int i; double d; }; ``` On a typical 64-bit system with default alignment, which of the following statements about `sizeof(struct Data)` is true?
IntermediateShow answer & explanation
It is larger than 13 bytes due to padding inserted by the compiler for alignment.
The compiler inserts padding between members to align them to memory boundaries (e.g., `int` on a 4-byte boundary, `double` on an 8-byte boundary). This makes the structure size larger than the sum of its members' sizes. The exact size depends on the system, but it will be greater than 13 bytes.
Q30. What is the primary advantage of passing a pointer to a structure to a function instead of passing the structure by value?
IntermediateShow answer & explanation
It allows the function to modify the original structure.
Passing a pointer allows the function to access and modify the original structure via dereferencing (using `->`). Passing by value creates a copy, so modifications inside the function do not affect the original. While passing a pointer can be more efficient for large structures, that is not the primary advantage; the m…
Q31. Which of the following code snippets correctly defines a structure with an alias using `typedef` and then declares a variable of that type?
IntermediateShow answer & explanation
```c typedef struct { int x; int y; } Point; Point p; ```
Option A uses the correct syntax: `typedef struct { ... } Point;` which defines an anonymous structure and gives it the alias `Point`. Then `Point p;` declares a variable. Option B is missing the alias name in the typedef. Option C uses `typedef struct Point { ... };` without an alias, so `Point` is not defined. Option…
Topper level · 5 questions
Q32. Consider the following code snippet: ```c struct Node { int data; struct Node *next; }; void insertAfter(struct Node *prev, int value) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = prev->next; prev->next = newNode; } ``` If `head` points to the first node of a linked list and we call `insertAfter(head, 5)`, what is the effect on the list?
AdvancedShow answer & explanation
The new node is inserted after the first node, and the original second node becomes the third node.
The function `insertAfter` creates a new node, sets its `next` to `prev->next` (which is the original second node), and then sets `prev->next` to the new node. Thus, the new node is inserted immediately after `prev` (the first node), and the original second node becomes the third node. This is a standard linked-list in…
Q33. Given the following structure and function: ```c struct Employee { char name[50]; int id; float salary; }; void updateSalary(struct Employee *e, float newSalary) { e->salary = newSalary; } ``` Which of the following statements is true about the function `updateSalary`?
AdvancedShow answer & explanation
It modifies the salary of the original structure because it receives a pointer to the structure.
The function takes a pointer to `struct Employee` and uses the arrow operator to access and modify the `salary` member. Since it operates on the memory location pointed to by `e`, the change affects the original structure passed by the caller. This is a key advantage of passing structures by pointer: it avoids copying…
Q34. Consider the following structure and code: ```c struct Data { char a; int b; char c; }; int main() { struct Data d = {'x', 10, 'y'}; printf("%zu\n", sizeof(d)); return 0; } ``` On a typical 64-bit system with default alignment (where `int` is 4 bytes and alignment is 4 bytes), what will be the output?
AdvancedShow answer & explanation
12
The structure has members: `char a` (1 byte), `int b` (4 bytes), `char c` (1 byte). Due to alignment, the compiler inserts padding to align `int b` on a 4-byte boundary. Typically, `a` occupies offset 0, then 3 bytes of padding, `b` occupies offsets 4-7, `c` occupies offset 8, and then 3 bytes of padding at the end to…
Q35. Which of the following code snippets correctly defines a structure with an alias using `typedef` and then declares a variable of that type?
AdvancedShow answer & explanation
```c typedef struct { int x; int y; } Point; Point p; ```
Option A correctly uses `typedef` with an anonymous struct to create an alias `Point`, and then declares a variable `p` of that type. The other options have syntax errors: Option B uses `typedef` incorrectly (missing the type being aliased), Option C places `typedef` before the struct but doesn't provide an alias name,…
Q36. Consider the following code: ```c struct Student { char name[50]; int roll; }; void printStudent(struct Student s) { printf("%s %d\n", s.name, s.roll); } int main() { struct Student s1 = {"Alice", 101}; printStudent(s1); return 0; } ``` What is a potential drawback of passing `s1` to `printStudent` in this manner?
AdvancedShow answer & explanation
It copies the entire structure, which can be inefficient for large structures.
Passing a structure by value (as done here) creates a copy of the entire structure, including the `name` array (50 bytes) and the `roll` integer. For large structures, this copying overhead can be significant in terms of memory and time. The function operates on the copy, so the original `s1` is not modified. To avoid…
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages