Practice 20 Structures & Unions multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.
Show Answer & Explanation
Correct Answer: B — struct
The "struct" keyword is used to define a structure in C.
Show Answer & Explanation
Correct Answer: B — ptr->member
The arrow operator (->) is used to access structure members through a pointer.
Show Answer & Explanation
Correct Answer: B — Union members share memory
In a union, all members share the same memory location, whereas in a structure, each member has its own memory location.
Show Answer & Explanation
Correct Answer: B — 4 bytes
The size of a union is the size of its largest member. Here, int and float are both 4 bytes, so union size is 4 bytes (ignoring padding).
Show Answer & Explanation
Correct Answer: B — Yes, it is called self-referential structure
A self-referential structure contains a pointer to the same structure type. This is commonly used in linked lists and trees.
What is the output?
`struct Point { int x, y; };`
`struct Point p = {10, 20};`
`printf("%d", p.x);`Show Answer & Explanation
Correct Answer: A — 10
Structure members are accessed using the dot operator. p.x gives the value 10.
Show Answer & Explanation
Correct Answer: B — typedef
typedef is used to create an alias for a data type, including structures. Example: typedef struct Point Point;
Show Answer & Explanation
Correct Answer: B — Adding empty bytes for memory alignment
Structure padding adds empty bytes between members to align data in memory for efficient CPU access.
How do you access a structure member using a pointer?
`struct Point *ptr;`Show Answer & Explanation
Correct Answer: B — ptr->x
The arrow operator (->) is used to access structure members through a pointer. ptr->x is equivalent to (*ptr).x
Show Answer & Explanation
Correct Answer: B — Yes
Yes, we can declare an array of structures. Example: struct Student students[50]; creates an array of 50 Student structures.
Show Answer & Explanation
Correct Answer: B — Structure containing another structure
A nested structure is a structure that contains another structure as one of its members.
What is the size of this structure?
`struct Test { char c; int i; };`
(Assuming 4-byte int and typical padding)Show Answer & Explanation
Correct Answer: B — 8 bytes
Due to padding, char (1 byte) + padding (3 bytes) + int (4 bytes) = 8 bytes for proper memory alignment.
Show Answer & Explanation
Correct Answer: C — .
The dot operator (.) is used to access members of a structure variable directly.
Show Answer & Explanation
Correct Answer: C — Memory location
In a union, all members share the same memory location. Only one member can hold a value at any given time.
What is the output?
`union Data { int i; float f; char c; };`
`union Data d; d.i = 65;`
`printf("%c", d.c);`Show Answer & Explanation
Correct Answer: B — A
Since union members share memory, the byte pattern of integer 65 is interpreted as character, which is "A" (ASCII 65).
Show Answer & Explanation
Correct Answer: C — Yes, by value or by pointer
Structures can be passed to functions both by value (a copy is made) and by pointer (address is passed).
Show Answer & Explanation
Correct Answer: C — Cleaner and shorter code
typedef allows you to avoid writing "struct" keyword every time. Example: Instead of "struct Point p;", you can write "Point p;"
Show Answer & Explanation
Correct Answer: B — A field that specifies number of bits to use
Bit fields allow specifying the number of bits a member should occupy. Example: unsigned int flag : 1; uses only 1 bit.
Show Answer & Explanation
Correct Answer: B — Yes
Yes, functions can return structures in C. The entire structure is copied when returning.
Show Answer & Explanation
Correct Answer: B — struct Point *ptr;
struct Point *ptr; declares ptr as a pointer to a structure of type Point.