Back to Practice C Programming

Structures & Unions - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

Structures & Unions Question Bank for C-CAT

Topic-wise Structures & Unions MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
What is the keyword used to define a structure in C?
Aclass
Bstruct
Cstructure
Dtype
Show Answer & Explanation

Correct Answer: B - struct

The "struct" keyword is used to define a structure in C.

Q2.
How do you access a member of a structure using a pointer?
Aptr.member
B*ptr.member
Cptr::member
Dptr->member
Show Answer & Explanation

Correct Answer: D - ptr->member

The arrow operator (->) is used to access structure members through a pointer.

Q3.
What is the difference between structure and union?
AUnion members share memory
BNo difference
CStructure members share memory
DUnion cannot have members
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.

Q4.
What is the size of a union with members int (4 bytes), char (1 byte), and float (4 bytes)?
A9 bytes
B1 byte
C4 bytes
DDepends on compiler
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).

Q5.
Can a structure contain a pointer to itself?
ANo, it causes error
BOnly with typedef
COnly in C++
DYes, it is called self-referential structure
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.

Q6.

What is the output?

struct Point { int x, y; };
struct Point p = {10, 20};
printf("%d", p.x);
ACompilation error
B20
C0
D10
Show Answer & Explanation

Correct Answer: D - 10

Structure members are accessed using the dot operator. p.x gives the value 10.

Q7.
Which keyword is used to define an alternate name for a structure?
Adefine
Brename
Calias
Dtypedef
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;

Q8.
What is structure padding?
AAdding extra members
BCompressing structure size
CRemoving unused members
DAdding empty bytes for memory alignment
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.

Q9.

How do you access a structure member using a pointer?

struct Point *ptr;
Aptr->x
Bptr.x
C*ptr.x
Dptr::x
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

Q10.
Can we have an array of structures in C?
ANo
BOnly with pointers
CYes
DOnly with typedef
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.

Q11.
What is a nested structure?
AStructure with no members
BStructure inside a function
CStructure with only pointers
DStructure containing another structure
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.

Q12.

What is the size of this structure?

struct Test { char c; int i; };
(Assuming 4-byte int and typical padding)
A5 bytes
B8 bytes
C4 bytes
D1 byte
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.

Q13.
Which operator is used to access structure members directly?
A.
B::
C->
D[]
Show Answer & Explanation

Correct Answer: A - .

The dot operator (.) is used to access members of a structure variable directly.

Q14.
In a union, all members share the same:
AName
BValue
CMemory location
DData type
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.

Q15.

What is the output?

union Data { int i; float f; char c; };
union Data d; d.i = 65;
printf("%c", d.c);
A65
BA
C0
DCompilation error
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).

Q16.
Can structures be passed to functions in C?
ANo
BYes, by value or by pointer
COnly by pointer
DOnly by value
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).

Q17.
What is the advantage of using typedef with structures?
AFaster execution
BCleaner and shorter code
CLess memory usage
DBetter security
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;"

Q18.
What is a bit field in structures?
AA boolean field
BA binary field
CA field that specifies number of bits to use
DA pointer field
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.

Q19.
Can a function return a structure in C?
ANo
BOnly in C99
CYes
DOnly by pointer
Show Answer & Explanation

Correct Answer: C - Yes

Yes, functions can return structures in C. The entire structure is copied when returning.

Q20.
What is the correct syntax to declare a structure pointer?
Astruct Point *ptr;
Bstruct Point ptr;
C*struct Point ptr;
Dpointer struct Point ptr;
Show Answer & Explanation

Correct Answer: A - struct Point *ptr;

struct Point *ptr; declares ptr as a pointer to a structure of type Point.

Q21.

What is the output?

struct Point { int x; int y; };
struct Point p = {10, 20};
printf("%d", p.y);
A10
BCompilation error
C0
D20
Show Answer & Explanation

Correct Answer: D - 20

The structure Point is initialized with x=10 and y=20. Accessing p.y returns 20.

Q22.

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;
};
A13
B4
C1
D8
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).

Q23.

What is the output?

typedef struct {
    int id;
    float marks;
} Student;
Student s = {101, 85.5};
printf("%d", s.id);
A85
B101
C0
DCompilation error
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.

Q24.
Which operator is used to access members of a structure through a pointer?
A.
B&
C::
D->
Show Answer & Explanation

Correct Answer: D - ->

The arrow operator (->) is used to access structure members through a pointer. It is equivalent to (*ptr).member.

Q25.

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);
A0
B20
C10
DCompilation error
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.

Q26.
What is the difference between a structure and a union in C?
AStructures can have pointers, unions cannot
BUnions are faster than structures
CIn a structure all members occupy separate memory, in a union all members share the same memory
DStructures cannot be nested
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.

Q27.

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);
A5 10
B10 10
C5 5
DCompilation error
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.

Q28.

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)
A12
B9
C8
D10
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.

Q29.

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);
A3 7
B3 3
C7 7
D7 3
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.

Q30.
Which keyword is used to create a type alias in C?
Atypedef
Bdefine
Calias
Dtypename
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'.

Q31.

What is the output?

union Val {
    int i;
    char c;
};
union Val v;
v.i = 65;
printf("%c", v.c);
A6
B5
CA
DCompilation error
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).

Q32.

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);
A5
B10
C0
DCompilation error
Show Answer & Explanation

Correct Answer: A - 5

This demonstrates nested structures. The inner structure is accessed using o.in.x, which gives 5.

Q33.

What does the following typedef accomplish?

typedef int (*FuncPtr)(int, int);
ADeclares a function
BCreates an alias for a function pointer type
CDefines a macro
DDeclares an array of integers
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.

Q34.

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);
AAmit is 0
BCompilation error
CRuntime error
DAmit is 25
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".

Q35.

What is wrong with the following code?

struct Node {
    int data;
    struct Node next;
};
ANothing is wrong
BA structure cannot contain an instance of itself
CMissing semicolon
Ddata should be a pointer
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.

Q36.

What is the output?

struct Data {
    int arr[3];
};
struct Data d = {{10, 20, 30}};
printf("%d", d.arr[1]);
A20
B10
C30
DCompilation error
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.

Q37.
What happens when you use the assignment operator between two structures of the same type?
ACompilation error
BA shallow copy of all members is performed
COnly pointers are copied
DA deep copy is performed
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.

Q38.

What is the output?

struct Coord {
    int x, y;
};
struct Coord points[3] = {{1,2}, {3,4}, {5,6}};
printf("%d", points[2].x);
A1
B3
C5
D6
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.

Q39.

What is the correct way to dynamically allocate a structure?

struct Student { int id; float gpa; };
Astruct Student *s = malloc(Student);
Bstruct Student *s = malloc(sizeof(struct Student));
Cstruct Student *s = new Student;
Dstruct Student s = calloc(1, sizeof(Student));
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.

Q40.

What is a bit field in C structures?

struct Flags {
    unsigned int bold : 1;
    unsigned int italic : 1;
    unsigned int underline : 1;
};
AAn array of bits
BA pointer to a bit
CA boolean flag
DA way to specify the number of bits a member should occupy
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.

Q41.

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);
A11
B10
C0
DCompilation error
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.

Q42.

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);
A4 6
B3 4
C1 2
DCompilation error
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.

Q43.

What is the purpose of the 'packed' attribute?

struct __attribute__((packed)) Data {
    char a;
    int b;
};
AEncrypts the structure data
BMakes the structure read-only
CRemoves padding between members
DIncreases alignment
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.

Q44.

What is the output?

union Mixed {
    int i;
    float f;
};
union Mixed m;
m.f = 3.14f;
printf("%d", m.i);
AAn unpredictable integer value
B3.14
C3
D0
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.

Q45.

What is the output?

struct Config {
    int enabled;
    int level;
};
struct Config cfg = {.level = 5, .enabled = 1};
printf("%d %d", cfg.enabled, cfg.level);
A5 1
B1 5
C0 0
DCompilation error
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.

Q46.

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);
APen: 50
BCompilation error
CRuntime error
DPen: 0
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".

Q47.

How many bytes does this structure occupy?

struct Bits {
    unsigned int a : 3;
    unsigned int b : 5;
    unsigned int c : 8;
};
A16
B2
C4
D3
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.

Q48.

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);
A40
B30
C50
D10
Show Answer & Explanation

Correct Answer: A - 40

n.inner.a is 10 and n.c is 30. Their sum is 10 + 30 = 40.

Q49.

What is a flexible array member in C99?

struct Buffer {
    int length;
    char data[];
};
AAn array that can shrink
BA pointer disguised as an array
CAn incomplete array at the end of a structure for variable-length data
DAn array initialized to zero
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).

Q50.

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);
A0 0
B1 2
C2 1
DCompilation error
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.

Showing 1-10 of 50 questions