MCQs on Structures and Unions | C Programming

Defining and Declaring Structures

  1. How do you define a structure in C? a) struct name {type member1; type member2;}
    b) structure name {type member1; type member2;}
    c) struct name[] {type member1; type member2;}
    d) structure name() {type member1; type member2;}
  2. What is the correct syntax to declare a structure variable? a) struct var_name;
    b) struct type var_name;
    c) type struct var_name;
    d) structure type var_name;
  3. Which keyword is used to define a structure in C? a) struct
    b) structure
    c) type
    d) object

Accessing Structure Members

  1. How do you access a structure member in C?
    a) . (dot operator)
    b) -> (arrow operator)
    c) [] (array index operator)
    d) Both a and b
  2. What is the correct way to access a structure member through a pointer? a) ptr.member
    b) ptr->member
    c) ptr[member]
    d) ptr.member[]
  3. What is the result of this code?
    struct Person { char name[30]; int age; };
    struct Person p1 = {"Alice", 25};
    printf("%s", p1.name);
    a) Prints “Alice”
    b) Prints “25”
    c) Prints the memory address of p1
    d) Compilation error

Nested Structures

  1. What is a nested structure?
    a) A structure that contains an array of elements
    b) A structure that contains another structure as a member
    c) A structure inside another structure of the same type
    d) A structure that has multiple members of the same type
  2. How do you access a member of a nested structure?
    a) . (dot operator)
    b) -> (arrow operator)
    c) Both a and b
    d) [] (array index operator)
  3. In which case can nested structures be useful?
    a) When defining structures with arrays as members
    b) When defining a structure to represent complex data that requires multiple levels
    c) For simple structures
    d) None of the above

Arrays of Structures

  1. How do you declare an array of structures in C?
    a) struct Person arr[10];
    b) struct Person[10] arr;
    c) Person arr[10];
    d) struct[10] Person arr;
  2. How do you access the elements of an array of structures?
    a) arr.member
    b) arr[i].member
    c) arr(i).member
    d) arr.member[i]
  3. What happens when you initialize an array of structures?
    a) All members are initialized to zero by default
    b) Each structure element must be initialized explicitly
    c) Only the first element is initialized
    d) None of the above

Structure Pointers

  1. How do you declare a pointer to a structure?
    a) struct Person *ptr;
    b) struct *ptr;
    c) ptr->Person;
    d) ptr: Person*;
  2. What is the correct way to access a structure member through a pointer?
    a) ptr->member
    b) ptr.member
    c) ptr[member]
    d) *ptr.member
  3. Which of the following is the correct syntax for using a structure pointer?
    a) struct Person *ptr = &p1; ptr->age = 30;
    b) struct *ptr = p1; ptr->age = 30;
    c) ptr = p1; ptr->age = 30;
    d) ptr->age = 30;
  4. In the statement ptr->name = "Alice";, what does ptr represent?
    a) A structure variable
    b) A structure pointer
    c) A regular variable
    d) A pointer to a character array

Unions and Differences with Structures

  1. What is the main difference between a structure and a union in C?
    a) A structure stores values of all its members simultaneously, while a union stores only one member at a time
    b) A structure stores only one value, while a union stores all values
    c) Both structures and unions store values in the same way
    d) Unions are used for storing arrays, while structures are used for variables
  2. How do you declare a union?
    a) union name { type member1; type member2; };
    b) union name { type member1, member2; };
    c) structure name { type member1; };
    d) union name[] { type member1; };
  3. In a union, what happens when a new member is assigned a value? a) All members of the union are assigned the same value
    b) Only the member assigned the value holds it; other members are overwritten
    c) All members retain their values
    d) The program throws an error
  4. How do you access a union member in C?
    a) Using the . (dot) operator
    b) Using the -> (arrow) operator
    c) Using array indexing
    d) Both a and b
  5. When using a union, how much memory does it take?
    a) The memory size of the largest member
    b) The memory size of all members
    c) The size of the smallest member
    d) None of the above
  6. What is the correct syntax for accessing a union member?
    a) union_name.member
    b) union_name->member
    c) member.union_name
    d) member[union_name]

Enumerations (enum)

  1. What is an enumeration in C?
    a) A way to define a variable that can hold multiple values
    b) A user-defined data type that consists of a set of named integer constants
    c) A variable of type float
    d) A structure of named integer constants
  2. How do you define an enumeration in C?
    a) enum name { value1, value2, value3 };
    b) enum name { value1, value2 };
    c) enum name(value1, value2);
    d) enum { value1, value2 };
  3. What is the default value of the first item in an enumeration?
    a) 1
    b) 0
    c) -1
    d) Undefined
  4. How can you assign custom values to the members of an enumeration?
    a) enum name { value1 = 10, value2 = 20 };
    b) enum name { 10 = value1, 20 = value2 };
    c) enum name { value1, value2 = 20 };
    d) enum name { 10, 20 = value2 };
  5. How do you access an enumeration value?
    a) enum_name.value
    b) value.enum_name
    c) enum.value
    d) enum_name[value]
  6. Which of the following is true about enumerations in C?
    a) The values of enumeration constants are automatically assigned
    b) Enumerations cannot store string values
    c) Enumeration values cannot be explicitly assigned
    d) All of the above
  7. What is the default data type for enumeration values in C?
    a) float
    b) char
    c) int
    d) double
  8. What will be the value of day in this code?
num days { Monday = 1, Tuesday, Wednesday };
enum days day = Wednesday;

a) 0
b) 1
c) 2
d) 3

Additional MCQs

  1. Can a structure have members of different data types?
    a) Yes
    b) No
    c) Only integer and character types
    d) Only floating-point types
  2. How many bytes of memory does a structure take?
    a) The size of the largest member
    b) The sum of the sizes of all members
    c) Depends on the data types of members
    d) The size of the first member
  3. Which of the following is true about union members?
    a) All members occupy separate memory locations
    b) All members of the union share the same memory location
    c) Unions cannot have members of the same data type
    d) None of the above
  4. Which of the following is not allowed in structures?
    a) Arrays as members
    b) Pointers as members
    c) Functions as members
    d) Structures as members
  5. Which of the following will output “Yellow”?
enum color { Red = 1, Green = 2, Yellow = 3 };
printf("%d", Yellow);

a) 1
b) 2
c) 3
d) Compilation error

  1. What happens when two structures share the same name in different scopes?
    a) They are treated as the same structure
    b) They are treated as different structures
    c) Compilation error
    d) One structure is ignored
  2. Which of the following is true about pointers to structures?
    a) Structure pointers can be declared like any other pointer
    b) Structure pointers can only point to variables of the same structure type
    c) Structure pointers cannot be dereferenced
    d) None of the above
  3. What is the size of the following structure?
struct MyStruct { int x; char y; };

a) 4 bytes
b) 5 bytes
c) 8 bytes
d) 6 bytes

  1. What will be printed by this code?
union Data { int i; float f; };
union Data data;
data.i = 10;
printf("%f", data.f);

a) 10
b) Undefined value
c) 0
d) Compilation error

  1. Which of the following is true about enumerations?
    a) An enumeration is automatically an integer type
    b) Enumeration constants are of type enum
    c) You can assign non-integer values to enumerations
    d) Enumerations cannot be used in switch cases
  2. What happens when you print a structure directly using printf?
    a) It outputs all members of the structure
    b) It prints a memory address
    c) It causes a compilation error
    d) It prints the first member only
  3. Which is the correct syntax for initializing a structure?
    a) struct Person p1 = {"Alice", 25};
    b) struct Person p1; p1.name = "Alice"; p1.age = 25;
    c) p1 = {"Alice", 25};
    d) Both a and b
  4. Can a union hold different data types at the same time?
    a) Yes
    b) No
    c) Only strings and integers
    d) Only primitive types
  5. Which of the following is the correct syntax for creating an array of unions?
    a) union Data arr[10];
    b) union arr[10];
    c) union Data[] arr;
    d) arr[union Data];
  6. What is the memory size of a union if the members are: int x; char y; float z;
    a) 4 bytes
    b) 8 bytes
    c) 12 bytes
    d) 10 bytes
  7. Which of the following types can be used as members in an enumeration?
    a) int
    b) float
    c) char
    d) All of the above
  8. Can an enumeration value be assigned to a non-integer variable?
    a) Yes
    b) No
    c) Only char types
    d) Only float types
  9. Which of the following is true for structures in C?
    a) Structure members can be of any data type
    b) Structures always occupy contiguous memory locations
    c) Structures cannot be nested
    d) Structures can only contain integer data types
  10. What is a characteristic of a union in C?
    a) It can hold only one value at a time
    b) It uses memory for all its members
    c) Each member has its own memory location
    d) None of the above
  11. How can an enumeration value be compared?
    a) Using == operator
    b) Using = operator
    c) Using != operator
    d) Both a and c

QnoAnswer
1a) struct name {type member1; type member2;}
2b) struct type var_name
3a) struct
4d) Both a and b
5b) ptr->member
6a) Prints “Alice”
7b) A structure that contains another structure as a member
8c) Both a and b
9b) When defining a structure to represent complex data that requires multiple levels
10a) struct Person arr[10];
11b) arr[i].member
12a) All members are initialized to zero by default
13a) struct Person *ptr;
14a) ptr->member
15a) struct Person *ptr = &p1; ptr->age = 30;
16b) A structure pointer
17a) A structure stores values of all its members simultaneously, while a union stores only one member at a time
18a) union name { type member1; type member2; };
19b) Only the member assigned the value holds it; other members are overwritten
20a) Using the . (dot) operator
21a) The memory size of the largest member
22a) union_name.member
23b) A user-defined data type that consists of a set of named integer constants
24a) enum name { value1, value2, value3 };
25b) 0
26a) enum name { value1 = 10, value2 = 20 };
27a) enum_name.value
28b) Enumerations cannot store string values
29c) int
30c) 2
31a) Yes
32b) The sum of the sizes of all members
33b) All members of the union share the same memory location
34c) Functions as members
35c) 3
36b) They are treated as different structures
37a) Structure pointers can be declared like any other pointer
38c) 8 bytes
39b) Undefined value
40a) An enumeration is automatically an integer type
41c) It causes a compilation error
42d) Both a and b
QnoAnswer
43b) No
44a) union Data arr[10];
45b) 8 bytes
46d) All of the above
47b) No
48a) Structure members can be of any data type
49a) It can hold only one value at a time
50d) Both a and c

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top