C Programming

Structures & Unions — Practice MCQs for CCAT

20 Questions Section B: Programming C Programming

Practice 20 Structures & Unions multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.

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
Bptr->member
Cptr::member
D*ptr.member
Show Answer & Explanation

Correct Answer: B — ptr->member

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

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

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

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

Q6.

What is the output?


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

Correct Answer: A — 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
Btypedef
Calias
Drename
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;

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

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: B — 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
BYes
COnly with pointers
DOnly with typedef
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.

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

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: C — .

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
BOnly by pointer
CYes, by value or by pointer
DOnly by value
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).

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

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

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

Correct Answer: B — 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: B — struct Point *ptr;

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