Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

499 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 499
Topics in C Programming
Q.171 Medium Structures & Unions
How can you create a structure that can hold either an integer or a floating-point number, but not both simultaneously?
A Using a structure with conditional compilation
B Using a union instead of a structure
C Using an array of mixed types
D Using a pointer to void
Correct Answer:  B. Using a union instead of a structure
EXPLANATION

A union is the appropriate choice when you need to store different data types but only one value at a time.

Take Test
Q.172 Medium Structures & Unions
What is the primary advantage of using unions in embedded systems?
A Reduced memory consumption
B Faster execution
C Better readability
D Easier debugging
Correct Answer:  A. Reduced memory consumption
EXPLANATION

Unions are primarily used in embedded systems to conserve memory by allowing multiple members to share the same memory location.

Take Test
Q.173 Medium Structures & Unions
Which of the following is a valid declaration of nested structure?
A struct A { int x; struct B { int y; } b; };
B struct A { int x; struct B b; };
C Both A and B are valid
D Neither A nor B is valid
Correct Answer:  C. Both A and B are valid
EXPLANATION

Both nested structure definitions (defining B inside A and declaring B as member) are valid in C.

Take Test
Q.174 Medium Structures & Unions
What happens if you attempt to assign one structure variable to another of the same type?
A Compilation error occurs
B All members are copied
C Only pointers are copied
D Runtime error occurs
Correct Answer:  B. All members are copied
EXPLANATION

In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).

Take Test
Q.175 Medium Structures & Unions
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.

Take Test
Q.176 Medium Structures & Unions
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.

Take Test
Q.177 Medium Structures & Unions
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.

Take Test
Q.178 Medium Structures & Unions
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.

Take Test
Q.179 Medium Structures & Unions
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.

Take Test
Q.180 Medium Structures & Unions
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.

Take Test
IGET
iget AI
Online · Ask anything about exams
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