Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 171–180 of 490
Topics in C Programming
Q.171 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.

Test
Q.172 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.

Test
Q.173 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).

Test
Q.174 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.

Test
Q.175 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.

Test
Q.176 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.

Test
Q.177 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.

Test
Q.178 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.

Test
Q.179 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.

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