4 Lecture
CS410
Midterm & Final Term Short Notes
Structures and Unions
Structures and unions are fundamental data types in programming. Structures group variables of different types, creating custom data structures. Unions allow sharing memory among variables to save space. Both are essential for efficient memory m
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
1. What is a structure in C/C++?
a) A set of related functions
b) A collection of variables of different data types
c) A control flow statement
d) A loop construct
Solution: b) A collection of variables of different data types
2. How do you access a member inside a structure in C/C++?
a) Using the dot (.) operator
b) Using the arrow (->) operator
c) Using the at (@) symbol
d) Using the pound (#) symbol
Solution: a) Using the dot (.) operator
3. What is the size of an empty structure in C/C++?
a) 0 bytes
b) 1 byte
c) 4 bytes
d) Depends on the architecture of the machine
Solution: b) 1 byte
4. What is the purpose of unions in C/C++?
a) To define custom data types
b) To group related variables
c) To save memory by sharing memory among variables
d) To implement conditional statements
Solution: c) To save memory by sharing memory among variables
5. Which operator is used to access a member inside a union in C/C++?
a) Dot (.) operator
b) Arrow (->) operator
c) Colon (:) operator
d) Double-colon (::) operator
Solution: a) Dot (.) operator
6. What happens if you modify one member of a union and then access another member?
a) It is not allowed to modify union members individually
b) The other member retains its old value
c) It results in an error
d) The behavior is undefined
Solution: d) The behavior is undefined
7. Which statement is true about the alignment of structure members?
a) All members are aligned at even memory addresses
b) The alignment depends on the order of declaration
c) The alignment is automatic and doesn't follow any rule
d) The alignment depends on the data type of the members
Solution: d) The alignment depends on the data type of the members
8. What is the keyword used to define a union in C/C++?
a) class
b) structure
c) union
d) typedef
Solution: c) union
9. Can a structure have another structure as its member in C/C++?
a) Yes, but only one level deep
b) No, structures cannot have other structures as members
c) Yes, there is no such limitation
d) Only if the structure is empty
Solution: c) Yes, there is no such limitation
10. What is the primary difference between a structure and a union in C/C++?
a) A structure can hold variables of different data types, but a union cannot.
b) A union can hold variables of different data types, but a structure cannot.
c) A structure and a union are the same; there is no difference.
d) The primary difference depends on the programming language being used.
Solution: a) A structure can hold variables of different data types, but a union cannot.
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
1. Question: What is a structure in C/C++?
Answer: A structure in C/C++ is a user-defined data type that allows grouping multiple variables of different data types under a single name.
2. Question: How do you declare a structure in C/C++?
Answer: To declare a structure, you use the `struct` keyword followed by the structure's name and a list of its member variables.
3. Question: What is the difference between a structure and an array in C/C++?
Answer: Unlike an array, a structure can hold variables of different data types, while all elements in an array must be of the same data type.
4. Question: How do you access a member inside a structure in C/C++?
Answer: You can access a member inside a structure using the dot (.) operator followed by the member's name.
5. Question: What is a union in C/C++?
Answer: A union is a user-defined data type that allows multiple variables to share the same memory space, helping to save memory when only one variable is used at a time.
6. Question: What happens if you modify one member of a union and then access another member?
Answer: Modifying one member and accessing another member of a union results in undefined behavior. The value retrieved will depend on the memory layout and can lead to unexpected results.
7. Question: How do you declare a union in C/C++?
Answer: To declare a union, you use the `union` keyword followed by the union's name and a list of its member variables.
8. Question: Can a union have functions as its members in C/C++?
Answer: No, unions can only have variables as their members, not functions.
9. Question: Can a structure have pointers as its members in C/C++?
Answer: Yes, a structure can have pointers as its members, allowing it to store memory addresses of other variables.
10. Question: What is the primary difference between a structure and a union in C/C++?
Answer: The primary difference is that a structure allocates memory for each of its members separately, while a union shares the same memory space for all its members, allowing only one member to be active at any given time.