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.
Which of the following correctly defines a structure named 'Point' with integer members 'x' and 'y' in C?
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.
How do you access the 'age' member of a structure variable named 'person' in C?
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.
Which statement best describes how memory is allocated for members of a union in C?
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.
How would you correctly declare an enumeration type named 'Day' with members MON, TUE, and WED in C?
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.
If you declare 'enum Color { RED, GREEN, BLUE };', what is the value of GREEN?
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.
Given a pointer 'pt' to a structure, how do you access its member 'x'?
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.
Which of the following statements is true about structures within structures (nested structures) in C?
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.
What happens when one structure variable is assigned to another variable of the same structure type in C?
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.
After assigning a value to one union member, what value can you expect other members to hold?
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.
Which statement about enumerations (enums) in C is correct?
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.