Structures & Unions Question Bank for C-CAT
Topic-wise Structures & Unions MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - struct
The "struct" keyword is used to define a structure in C.
Show Answer & Explanation
Correct Answer: D - ptr->member
The arrow operator (->) is used to access structure members through a pointer.
Show Answer & Explanation
Correct Answer: A - 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: C - 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: D - 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: D - 10
Structure members are accessed using the dot operator. p.x gives the value 10.
Show Answer & Explanation
Correct Answer: D - typedef
typedef is used to create an alias for a data type, including structures. Example: typedef struct Point Point;
Show Answer & Explanation
Correct Answer: D - 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: A - 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: C - 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: D - 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: A - .
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: B - 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: B - 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: C - 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: C - Yes
Yes, functions can return structures in C. The entire structure is copied when returning.
Show Answer & Explanation
Correct Answer: A - struct Point *ptr;
struct Point *ptr; declares ptr as a pointer to a structure of type Point.
What is the output?
struct Point { int x; int y; };
struct Point p = {10, 20};
printf("%d", p.y);Show Answer & Explanation
Correct Answer: D - 20
The structure Point is initialized with x=10 and y=20. Accessing p.y returns 20.
What is the size of this union on a system where int is 4 bytes and double is 8 bytes?
union Data {
int i;
double d;
char c;
};Show Answer & Explanation
Correct Answer: D - 8
A union's size is determined by its largest member. The largest member is double (8 bytes), so sizeof(union Data) is 8 (possibly with padding for alignment).
What is the output?
typedef struct {
int id;
float marks;
} Student;
Student s = {101, 85.5};
printf("%d", s.id);Show Answer & Explanation
Correct Answer: B - 101
typedef creates an alias 'Student' for the anonymous struct. The variable s is initialized with id=101 and marks=85.5. s.id prints 101.
Show Answer & Explanation
Correct Answer: D - ->
The arrow operator (->) is used to access structure members through a pointer. It is equivalent to (*ptr).member.
What is the output?
struct Node {
int data;
struct Node *next;
};
struct Node n1 = {10, NULL};
struct Node n2 = {20, &n1};
printf("%d", n2.next->data);Show Answer & Explanation
Correct Answer: C - 10
n2.next points to n1. Accessing n2.next->data retrieves n1.data, which is 10. This is a self-referential structure used in linked lists.
Show Answer & Explanation
Correct Answer: C - In a structure all members occupy separate memory, in a union all members share the same memory
In a structure, each member has its own memory location. In a union, all members share the same memory space, and only one member can hold a value at a time.
What is the output?
struct Box {
int length;
int width;
};
struct Box b1 = {5, 3};
struct Box b2 = b1;
b2.length = 10;
printf("%d %d", b1.length, b2.length);Show Answer & Explanation
Correct Answer: A - 5 10
Structure assignment creates a copy. Changing b2.length does not affect b1. So b1.length remains 5 while b2.length becomes 10.
What will be the output?
struct Test {
int a;
char b;
int c;
};
printf("%lu", sizeof(struct Test));(Assume 4-byte int, 1-byte char, 4-byte alignment)
Show Answer & Explanation
Correct Answer: A - 12
Due to structure padding for alignment, the char member is padded to 4 bytes. Layout: int(4) + char(1) + padding(3) + int(4) = 12 bytes.
What is the output?
struct Pair {
int a, b;
};
void swap(struct Pair *p) {
int temp = p->a;
p->a = p->b;
p->b = temp;
}
struct Pair p = {3, 7};
swap(&p);
printf("%d %d", p.a, p.b);Show Answer & Explanation
Correct Answer: D - 7 3
The function receives a pointer to the structure and swaps its members. After calling swap(), p.a becomes 7 and p.b becomes 3.
Show Answer & Explanation
Correct Answer: A - typedef
typedef is used to create an alias for an existing type. For example, typedef int Integer; makes 'Integer' an alias for 'int'.
What is the output?
union Val {
int i;
char c;
};
union Val v;
v.i = 65;
printf("%c", v.c);Show Answer & Explanation
Correct Answer: C - A
In a union, i and c share the same memory. Setting v.i = 65 puts the value 65 in memory. Reading v.c interprets the lowest byte as a char, which is 'A' (ASCII 65).
What is the output?
struct Inner {
int x;
};
struct Outer {
struct Inner in;
int y;
};
struct Outer o = {{5}, 10};
printf("%d", o.in.x);Show Answer & Explanation
Correct Answer: A - 5
This demonstrates nested structures. The inner structure is accessed using o.in.x, which gives 5.
What does the following typedef accomplish?
typedef int (*FuncPtr)(int, int);Show Answer & Explanation
Correct Answer: B - Creates an alias for a function pointer type
This typedef creates an alias 'FuncPtr' for a pointer to a function that takes two int parameters and returns an int.
What is the output?
struct Employee {
char name[20];
int age;
};
struct Employee e;
strcpy(e.name, "Amit");
e.age = 25;
printf("%s is %d", e.name, e.age);Show Answer & Explanation
Correct Answer: D - Amit is 25
strcpy copies "Amit" into e.name, and e.age is set to 25. The printf outputs "Amit is 25".
What is wrong with the following code?
struct Node {
int data;
struct Node next;
};Show Answer & Explanation
Correct Answer: B - A structure cannot contain an instance of itself
A structure cannot contain an instance of itself because the compiler cannot determine its size. It must use a pointer: struct Node *next; This is called a self-referential structure.
What is the output?
struct Data {
int arr[3];
};
struct Data d = {{10, 20, 30}};
printf("%d", d.arr[1]);Show Answer & Explanation
Correct Answer: A - 20
The structure contains an array member. d.arr[1] accesses the second element of the array, which is 20.
Show Answer & Explanation
Correct Answer: B - A shallow copy of all members is performed
Structure assignment in C performs a shallow (member-wise) copy. All members including arrays are copied, but pointers still point to the same memory.
What is the output?
struct Coord {
int x, y;
};
struct Coord points[3] = {{1,2}, {3,4}, {5,6}};
printf("%d", points[2].x);Show Answer & Explanation
Correct Answer: C - 5
points[2] accesses the third element of the array, which is {5,6}. The x member of this element is 5.
What is the correct way to dynamically allocate a structure?
struct Student { int id; float gpa; };Show Answer & Explanation
Correct Answer: B - struct Student *s = malloc(sizeof(struct Student));
malloc(sizeof(struct Student)) allocates the correct number of bytes for the structure and returns a pointer to the allocated memory.
What is a bit field in C structures?
struct Flags {
unsigned int bold : 1;
unsigned int italic : 1;
unsigned int underline : 1;
};Show Answer & Explanation
Correct Answer: D - A way to specify the number of bits a member should occupy
Bit fields allow specifying the exact number of bits a structure member occupies. Here, each flag uses only 1 bit, saving memory.
What is the output?
struct Counter {
int count;
};
void increment(struct Counter c) {
c.count++;
}
struct Counter c = {10};
increment(c);
printf("%d", c.count);Show Answer & Explanation
Correct Answer: B - 10
Structures are passed by value in C. The function receives a copy, so the increment inside the function does not affect the original. c.count remains 10.
What is the output?
typedef struct {
int x;
int y;
} Vec2;
Vec2 add(Vec2 a, Vec2 b) {
Vec2 r = {a.x+b.x, a.y+b.y};
return r;
}
Vec2 v = add((Vec2){1,2}, (Vec2){3,4});
printf("%d %d", v.x, v.y);Show Answer & Explanation
Correct Answer: A - 4 6
The add function returns a new Vec2 with x=1+3=4 and y=2+4=6. Compound literals (Vec2){1,2} create temporary structure values.
What is the purpose of the 'packed' attribute?
struct __attribute__((packed)) Data {
char a;
int b;
};Show Answer & Explanation
Correct Answer: C - Removes padding between members
The packed attribute tells the compiler to remove padding between structure members, minimizing memory usage at the potential cost of performance.
What is the output?
union Mixed {
int i;
float f;
};
union Mixed m;
m.f = 3.14f;
printf("%d", m.i);Show Answer & Explanation
Correct Answer: A - An unpredictable integer value
Accessing a union member that was not the last one written to reinterprets the bit pattern. The float 3.14 stored as IEEE 754 will produce an unpredictable integer value when read as int.
What is the output?
struct Config {
int enabled;
int level;
};
struct Config cfg = {.level = 5, .enabled = 1};
printf("%d %d", cfg.enabled, cfg.level);Show Answer & Explanation
Correct Answer: B - 1 5
C99 designated initializers allow initializing members by name in any order. cfg.enabled is set to 1 and cfg.level is set to 5.
What is the output?
struct Item {
char name[10];
int qty;
};
struct Item *p = malloc(sizeof(struct Item));
strcpy(p->name, "Pen");
p->qty = 50;
printf("%s: %d", p->name, p->qty);
free(p);Show Answer & Explanation
Correct Answer: A - Pen: 50
Memory is dynamically allocated for the structure. Members are accessed via the arrow operator. The output is "Pen: 50".
How many bytes does this structure occupy?
struct Bits {
unsigned int a : 3;
unsigned int b : 5;
unsigned int c : 8;
};Show Answer & Explanation
Correct Answer: C - 4
Bit fields a(3) + b(5) + c(8) = 16 bits = 2 bytes. However, since the underlying type is unsigned int (4 bytes), the structure is at least 4 bytes.
What is the output?
struct Nested {
struct {
int a;
int b;
} inner;
int c;
};
struct Nested n = {{10, 20}, 30};
printf("%d", n.inner.a + n.c);Show Answer & Explanation
Correct Answer: A - 40
n.inner.a is 10 and n.c is 30. Their sum is 10 + 30 = 40.
What is a flexible array member in C99?
struct Buffer {
int length;
char data[];
};Show Answer & Explanation
Correct Answer: C - An incomplete array at the end of a structure for variable-length data
A flexible array member is an array declared without a size at the end of a structure. Memory for it is allocated dynamically using malloc(sizeof(struct Buffer) + n).
What is the output?
typedef struct Node {
int val;
struct Node *left, *right;
} Node;
Node a={1,NULL,NULL}, b={2,NULL,NULL};
Node root = {0, &a, &b};
printf("%d %d", root.left->val, root.right->val);Show Answer & Explanation
Correct Answer: B - 1 2
root.left points to node a (val=1) and root.right points to node b (val=2). This demonstrates a simple binary tree structure.