Q.1 What is the size of a structure in C determined by?
Sum of sizes of all members
Largest member size
Number of members
Compiler-specific padding and alignment
Explanation - The size of a structure in C depends not only on the sum of its members but also on padding added for alignment purposes, which can vary by compiler.
Correct answer is: Compiler-specific padding and alignment
Q.2 Which of the following is true about unions in C?
All members share the same memory location
Each member has separate memory
Unions can have only one member
Union size is always 1 byte
Explanation - In a union, all members share the same memory location, and the size of the union is equal to the size of its largest member.
Correct answer is: All members share the same memory location
Q.3 Which operator is used to access members of a structure through a pointer?
. (dot)
& (address)
-> (arrow)
* (dereference)
Explanation - The '->' operator is used to access structure members when you have a pointer to the structure.
Correct answer is: -> (arrow)
Q.4 What is the correct way to define a structure in C?
struct { int x; float y; };
struct myStruct { int x; float y; };
structure myStruct { int x; float y; };
struct myStruct ( int x; float y; );
Explanation - The correct syntax for defining a structure includes the 'struct' keyword, a structure name, and the members enclosed in braces.
Correct answer is: struct myStruct { int x; float y; };
Q.5 If a union has members int a, float b, and char c[10], what is the size of the union?
Size of int + float + char array
Size of largest member
Size of smallest member
Always 1 byte
Explanation - The size of a union is determined by its largest member because all members share the same memory location.
Correct answer is: Size of largest member
Q.6 Which statement about structure initialization in C is correct?
You can initialize a structure only at declaration
You can initialize a structure at declaration or using assignment
Structures cannot be initialized
Only unions can be initialized
Explanation - Structures can be initialized at the time of declaration or assigned values later using the assignment operator.
Correct answer is: You can initialize a structure at declaration or using assignment
Q.7 Which of the following is valid in C?
struct S s1, s2;
union U u1, u2;
Both A and B
None of the above
Explanation - In C, multiple structure or union variables can be declared in a single statement.
Correct answer is: Both A and B
Q.8 Which keyword is used to define a union in C?
struct
union
enum
typedef
Explanation - The 'union' keyword is used to define a union in C, which allows multiple members to share the same memory location.
Correct answer is: union
Q.9 What happens when you assign a value to one member of a union?
Only that member gets updated
All members get updated
Largest member gets updated
Memory gets reallocated
Explanation - Assigning a value to one member of a union updates the shared memory, which may affect the interpretation of other members.
Correct answer is: Only that member gets updated
Q.10 Can a structure contain another structure in C?
Yes, as a member
No
Only as a pointer
Only in unions
Explanation - A structure can contain another structure as a member, either directly or via pointers.
Correct answer is: Yes, as a member
Q.11 What is the purpose of the typedef keyword with structures?
To define a union
To give an alias to the structure type
To define an array of structures
To initialize a structure
Explanation - The 'typedef' keyword allows creating a new type name for a structure for easier usage.
Correct answer is: To give an alias to the structure type
Q.12 Which of the following correctly declares an array of structures?
struct Student s[10];
struct Student s;
struct Student[10] s;
Student s[10];
Explanation - To declare an array of structures, you specify the structure type followed by the array name and size.
Correct answer is: struct Student s[10];
Q.13 Which is true about memory allocation of structures and unions?
Structures allocate memory for all members separately
Unions allocate memory only for the largest member
Both structures and unions allocate memory the same way
Unions allocate memory for all members
Explanation - Structures allocate memory for each member separately, while unions share memory among members, allocating space only for the largest member.
Correct answer is: Unions allocate memory only for the largest member
Q.14 What is the value of uninitialized structure members in C?
0
Garbage values
NULL
Depends on compiler
Explanation - Uninitialized structure members in C contain garbage values unless explicitly initialized.
Correct answer is: Garbage values
Q.15 Which of the following is a correct way to access a structure member?
s.member
s->member
*s.member
&s.member
Explanation - The dot operator (.) is used to access members of a structure variable.
Correct answer is: s.member
Q.16 Can a union have a structure as a member?
Yes
No
Only pointer to structure
Only array of structures
Explanation - A union can contain a structure as one of its members, just like other data types.
Correct answer is: Yes
Q.17 Which of the following affects structure alignment?
Member types
Order of members
Compiler settings
All of the above
Explanation - Alignment depends on the data types of members, their order, and compiler-specific rules for padding.
Correct answer is: All of the above
Q.18 What is the default value of a union member in C?
0
NULL
Garbage value
Depends on first member
Explanation - Like structures, union members are not automatically initialized in C, so they contain garbage values by default.
Correct answer is: Garbage value
Q.19 Can structures in C be passed to functions?
Yes, by value or reference
No, only unions can
Yes, but only by reference
Yes, but only by value
Explanation - Structures can be passed to functions either by value (copy) or by reference (pointer).
Correct answer is: Yes, by value or reference
Q.20 How do you declare a pointer to a structure?
struct S *ptr;
S ptr*;
struct S ptr*;
ptr S*;
Explanation - A pointer to a structure is declared using the structure type followed by an asterisk and the pointer name.
Correct answer is: struct S *ptr;
Q.21 Which of the following is correct syntax to define a union and declare a variable simultaneously?
union U { int a; float b; } u1;
union U u1 { int a; float b; };
union U { int a; float b; }; u1;
union { int a; float b; } u1;
Explanation - You can define a union and declare a variable of that type in the same statement using this syntax.
Correct answer is: union U { int a; float b; } u1;
Q.22 What is a self-referential structure?
A structure that contains a pointer to its own type
A structure that contains only integers
A structure that contains unions
A structure without members
Explanation - Self-referential structures contain a pointer to the same type of structure, often used in linked lists.
Correct answer is: A structure that contains a pointer to its own type
Q.23 Is it possible to have a structure inside a union in C?
Yes
No
Only arrays
Only pointers
Explanation - C allows a union to contain a structure as a member.
Correct answer is: Yes
Q.24 Which of the following is true about structures and unions in C?
Structures allocate separate memory for each member, unions share memory
Unions allocate separate memory for each member, structures share memory
Both allocate memory the same way
Neither allocates memory
Explanation - Structures allocate memory for each member individually, whereas unions share the same memory among all members.
Correct answer is: Structures allocate separate memory for each member, unions share memory
