C Structures, Unions, and Enums Fundamentals Quiz Quiz

Explore key concepts of structures, unions, and enumerations in C programming. This quiz helps you assess your understanding of how these data types function, their syntax, memory usage, and practical applications in C code.

  1. Identifying Structure Syntax

    Which of the following correctly defines a structure named 'Point' with integer members 'x' and 'y' in C?

    1. struct Point { int x; int y; };
    2. structure Point (int x; int y);
    3. struct { int x; int y; } Point;
    4. Point struct { int x, y; };

    Explanation: The correct syntax is 'struct Point { int x; int y; };' where 'struct' is used to define a new structure. The other options do not follow proper C syntax for defining structures: 'structure' is not a valid keyword, placing the name after the closing brace creates an anonymous struct, and 'Point struct' is not a proper order in C.

  2. Accessing Structure Members

    How do you access the 'age' member of a structure variable named 'person' in C?

    1. person:age
    2. person.age
    3. age.person
    4. person-u003Eage

    Explanation: You use the dot operator as in 'person.age' to access a structure member when you have a structure variable. The arrow operator, '-u003E', is used with structure pointers, not with structure variables. 'age.person' and 'person:age' are incorrect syntax in C.

  3. Memory Allocation in Unions

    Which statement best describes how memory is allocated for members of a union in C?

    1. Each member gets its own separate memory, and the total size is the sum of all members.
    2. Union members occupy twice the size of the largest member.
    3. All members share the same memory location, equal to the size of the largest member.
    4. Each member has memory equal to the smallest member.

    Explanation: A union allocates enough space for its largest member, and all members share this same memory space. Therefore, changing one affects the others. The sum or the size of the smallest member is never how unions work, and they do not use twice the size of the largest member.

  4. Declaring an Enum Type

    How would you correctly declare an enumeration type named 'Day' with members MON, TUE, and WED in C?

    1. enum Day [ MON, TUE, WED ];
    2. enum Day { MON, TUE, WED };
    3. enum { Day: MON, TUE, WED };
    4. Day enum = { MON, TUE, WED };

    Explanation: 'enum Day { MON, TUE, WED };' is the correct and standard way to declare an enumeration in C. Using square brackets, the colon, or placing 'enum' after the name are all incorrect syntaxes in the C language.

  5. Default Enum Values

    If you declare 'enum Color { RED, GREEN, BLUE };', what is the value of GREEN?

    1. 0
    2. 10
    3. 2
    4. 1

    Explanation: Enum constants in C are assigned increasing integer values starting from 0 unless specified. Therefore, RED is 0, GREEN is 1, and BLUE is 2. Options 0 and 2 are the values for RED and BLUE, while 10 is unrelated.

  6. Structure Pointer Member Access

    Given a pointer 'pt' to a structure, how do you access its member 'x'?

    1. pt:x
    2. pt.x
    3. *pt.x
    4. pt-u003Ex

    Explanation: With a structure pointer, use the arrow operator: 'pt-u003Ex'. The dot operator is used with structure variables, not pointers. '*pt.x' is not correct syntax, and 'pt:x' doesn't exist in C.

  7. Nested Structures

    Which of the following statements is true about structures within structures (nested structures) in C?

    1. A structure can contain another structure as a member.
    2. A structure must only have basic data types as members.
    3. Nested structures are not allowed in C.
    4. A structure can only reference another structure via pointers.

    Explanation: Structures can include other structures as members, allowing for complex data representation. There is no restriction that only basic types are allowed, and C explicitly allows for nesting structures. While pointers can reference structures, direct inclusion of another struct is fully valid.

  8. Assigning to Structs

    What happens when one structure variable is assigned to another variable of the same structure type in C?

    1. All members are copied from source to destination.
    2. Only addresses are copied.
    3. Assignment is not allowed between structure variables.
    4. Only the first member is copied.

    Explanation: Assigning one structure variable to another of the same type copies all members' values. C allows structure assignment as long as the types match. The assignment does not just copy the first member or addresses; copying structures is allowed directly.

  9. Union Member Access

    After assigning a value to one union member, what value can you expect other members to hold?

    1. Other members hold the previous values.
    2. All members hold the same value as assigned.
    3. They contain unpredictable or overlapping data.
    4. Other members are set to zero.

    Explanation: Since all union members share the same memory space, writing to one can make the contents of the others unpredictable unless their interpretations overlap exactly. Other members do not inherit the value, nor are they zeroed or preserved.

  10. Enumerations and Type Safety

    Which statement about enumerations (enums) in C is correct?

    1. Enums restrict variables to only the defined values.
    2. Enums assign integer values to identifiers, improving code readability.
    3. Enums cannot be used in switch statements.
    4. Enum variables must always be declared as unsigned.

    Explanation: In C, enums assign integer constants to identifiers for better readability and maintainability. They do not restrict variables strictly to those values, can definitely be used in switch cases, and do not require unsigned declaration. The distractors do not accurately describe how enums work in C.