Home Subjects C Programming Structures & Unions

C Programming
Structures & Unions

C language from basics to advanced placement prep

46 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 46
Topics in C Programming
Which feature of structures makes them suitable for implementing linked lists?
A Self-referential pointers
B Dynamic memory allocation
C Array of structures
D Structure padding
Correct Answer:  A. Self-referential pointers
EXPLANATION

Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.

Test
What is the size of the following structure on a 32-bit system?
struct Point { char c; int x; };
A 5 bytes
B 8 bytes
C 9 bytes
D 6 bytes
Correct Answer:  B. 8 bytes
EXPLANATION

Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.

Test
What is the primary use case for unions in embedded systems?
A Efficient type casting and hardware register mapping
B Storing multiple values simultaneously
C Organizing large data structures
D Improving code readability
Correct Answer:  A. Efficient type casting and hardware register mapping
EXPLANATION

Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.

Test
Which structure feature is most suitable for implementing a state machine with limited states?
A Bit fields
B Union with enum
C Nested structures
D Flexible arrays
Correct Answer:  A. Bit fields
EXPLANATION

Bit fields efficiently store boolean/limited-range states using minimal memory, ideal for state machines.

Test
When comparing nested structures, which access method is INVALID?
struct Address { char city[20]; };
struct Person { Address addr; };
Person p;
A p.addr.city
B (&p)->addr.city
C p->addr.city
D Both B and C valid
Correct Answer:  C. p->addr.city
EXPLANATION

p is a structure variable (not pointer), so arrow operator (->) cannot be used. Only dot operator works.

Test
What happens when you access a union member that wasn't the last one to be written?
A It returns the last written value
B It returns the correctly stored value for that member
C It returns garbage or bitwise interpretation of current memory
D Compilation error occurs
Correct Answer:  C. It returns garbage or bitwise interpretation of current memory
EXPLANATION

All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.

Test
What is the issue with bit fields in structures regarding portability?
A Bit field ordering is implementation-dependent
B Bit fields cannot be used with pointers
C Bit fields always consume 32 bits regardless of size
D Bit fields cannot be initialized
Correct Answer:  A. Bit field ordering is implementation-dependent
EXPLANATION

Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.

Test
What will be the output of this code?
union Data { int x; char y; };
Data d = {65};
d.y = 'A';
printf("%d", d.x);
A 65
B 97
C 65 then 97
D Undefined behavior
Correct Answer:  D. Undefined behavior
EXPLANATION

Modifying y changes the same memory location as x. Only the last byte of x is affected, leading to unpredictable output.

Test
What is the primary difference between passing a structure by value vs by pointer in function calls?
A Pointer passing creates a copy of the structure
B Value passing allows direct modification of original structure members
C Pointer passing is more memory efficient for large structures
D Value passing is faster for structures with more than 10 members
Correct Answer:  C. Pointer passing is more memory efficient for large structures
EXPLANATION

Passing by pointer avoids copying the entire structure, making it efficient for large data structures.

Test
How much memory (in bytes) will the following structure occupy on a 32-bit system?
struct Data { char c; int i; short s; };
A 7
B 9
C 12
D 8
Correct Answer:  C. 12
EXPLANATION

Due to padding/alignment: char(1) + padding(3) + int(4) + short(2) = 12 bytes on 32-bit systems.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips