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?

Beginner
A To group variables of different types under a single name B To perform arithmetic operations C To store a single data type D To replace arrays
Show 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?

Beginner
A `typedef` B `struct` C `enum` D `union`
Show 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?

Beginner
A When a structure variable is declared B When the program starts C When the structure is defined D When the structure is passed to a function
Show 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?

Beginner
A `->` B `.` C `::` D `#`
Show answer & explanation

`.`

The dot operator (`.`) is used to access members of a structure variable.

Q5. What does `ptr->member` mean in terms of dereferencing?

Beginner
A `*(ptr.member)` B `&(ptr->member)` C `(*ptr).member` D `ptr.member`
Show 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;`?

Beginner
A It creates a new structure called Student B It allocates memory for a Student C It makes `struct` optional in all declarations D It creates an alias `Student` for `struct Student`
Show 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?

Beginner
A Because structures always include a hidden pointer B Because of padding for memory alignment C Because of compiler optimizations D Because of runtime overhead
Show 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?

Beginner
A Pass a pointer to the structure B Pass the structure by reference using `&` in the function call C Pass the structure by value D Pass the structure as a global variable
Show 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;`)?

Beginner
A It creates a reference to s1 B It is not allowed in C C It copies all members from s1 to s2 D It compares the two structures
Show 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.

Beginner
A True B False
Show 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.

Beginner
A True B False
Show answer & explanation

True

`ptr->member` is exactly equivalent to `(*ptr).member`.

Q12. What is a structure in C?

Beginner
Show answer & explanation

—

It allows bundling related data together.

Q13. Does defining a structure allocate memory? Explain.

Beginner
Show answer & explanation

—

The definition is a blueprint.

Q14. How do you access a member of a structure variable?

Beginner
Show answer & explanation

—

The dot operator connects the variable and member.

Q15. How do you access a member through a pointer to a structure?

Beginner
Show answer & explanation

—

The arrow operator dereferences the pointer and accesses the member.

Q16. How can you create an alias for a structure?

Beginner
Show answer & explanation

—

This allows using `Student` instead of `struct Student`.

Q17. Is structure assignment allowed in C? What does it do?

Beginner
Show answer & explanation

—

For example, `s2 = s1;` copies all members.

Q18. How do you initialize a structure variable at declaration? Give an example.

Beginner
Show answer & explanation

—

Values must match the member types and order.

Q19. What is the difference between the dot operator and the arrow operator?

Beginner
Show answer & explanation

—

If you have a pointer, use `->`; if you have a variable, use `.`.

Q20. Can a structure contain an array as a member?

Beginner
Show answer & explanation

—

This makes structures versatile for modeling complex data.

Q21. How do you access a member of an array of structures?

Beginner
Show answer & explanation

—

First select the element, then the member.

Building up · 10 questions

Q22. What is the equivalence of `ptr->member`?

Intermediate
Show answer & explanation

—

The arrow operator is a shorthand for dereferencing and dot access.

Q23. Can structures be compared directly with `==`? Why or why not?

Intermediate
Show 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?

Intermediate
Show answer & explanation

—

Alignment improves access speed but increases size.

Q25. What is the advantage of passing a structure by pointer to a function?

Intermediate
Show answer & explanation

—

Passing by pointer is pass-by-reference.

Q26. What is the foundation for implementing linked lists in C?

Intermediate
Show 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; } ```

Intermediate
A Compilation error B 10 30 C 10 10 D 30 30
Show 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; }; ```

Intermediate
A `struct Student s = {"Alice", 101}; struct Student *ptr = &s; printf("%s", *ptr.name);` B `struct Student s = {"Alice", 101}; struct Student *ptr = &s; printf("%s", ptr.name);` C `struct Student s = {"Alice", 101}; struct Student *ptr = s; printf("%s", ptr.name);` D `struct Student s = {"Alice", 101}; struct Student *ptr = &s; printf("%s", ptr->name);`
Show 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?

Intermediate
A It is smaller than 13 bytes because the compiler packs members tightly. B It is larger than 13 bytes due to padding inserted by the compiler for alignment. C It is exactly equal to `sizeof(char) + sizeof(int) + sizeof(double)` = 13 bytes. D It is undefined behavior to call `sizeof` on a structure.
Show 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?

Intermediate
A It avoids the need to use the arrow operator inside the function. B It automatically initializes the structure to zero. C It allows the function to modify the original structure. D It makes the function run faster because pointers are always faster than values.
Show 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?

Intermediate
A ```c struct Point { int x; int y; }; typedef Point; Point p; ``` B ```c struct Point { int x; int y; }; typedef struct Point Point; struct Point p; ``` C ```c typedef struct Point { int x; int y; }; Point p; ``` D ```c typedef struct { int x; int y; } Point; Point p; ```
Show 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?

Advanced
A The new node is inserted at the beginning of the list. B The new node replaces the first node, and the original first node is lost. C The new node is inserted after the first node, and the original second node becomes the third node. D The new node is inserted at the end of the list.
Show 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`?

Advanced
A It modifies a copy of the structure, so the original remains unchanged. B It will not compile because you cannot use the arrow operator on a pointer to a structure. C It only works if the structure is defined globally. D It modifies the salary of the original structure because it receives a pointer to the structure.
Show 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?

Advanced
A 16 B 6 C 12 D 8
Show 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?

Advanced
A ```c typedef struct { int x; int y; } Point; Point p; ``` B ```c struct Point { int x; int y; }; typedef Point; Point p; ``` C ```c struct Point { int x; int y; } typedef Point; Point p; ``` D ```c typedef struct Point { int x; int y; }; struct Point p; ```
Show 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?

Advanced
A It will not compile because structures cannot be passed to functions by value. B It modifies the original structure `s1` because the function receives a reference. C It copies the entire structure, which can be inefficient for large structures. D It only works if the structure is defined inside the function.
Show 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…

Make your own

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