Home Subjects C Programming Structures & Unions

C Programming
Structures & Unions

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 100
Topics in C Programming
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
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
What is the output of the following?
struct S { int a:3; int b:3; int c:3; };
printf("%zu", sizeof(struct S));
A 1
B 4
C 12
D Implementation dependent
Correct Answer:  D. Implementation dependent
EXPLANATION

Bit field packing is implementation-defined. Size depends on compiler's bit field allocation strategy.

Take Test
Which of the following correctly demonstrates self-referential structure (for linked list)?
A struct Node { int data; struct Node *next; };
B struct Node { int data; Node *next; };
C struct Node { int data; *next; };
D Node { int data; Node *next; };
Correct Answer:  A. struct Node { int data; struct Node *next; };
EXPLANATION

In C, self-referential structures require the struct keyword in pointer declarations within the structure definition.

Take 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.

Take Test
In a structure with flexible array members, which statement is INCORRECT?
struct FlexArray { int len; int arr[]; };
A Flexible array member must be the last member
B Structure can be allocated on stack directly
C Memory for array must be allocated separately
D It is a C99 standard feature
Correct Answer:  B. Structure can be allocated on stack directly
EXPLANATION

Flexible array members require dynamic allocation; the structure itself cannot be allocated on the stack with a defined size for the array.

Take 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.

Take Test
Which of these is a valid declaration of a structure variable using dot operator immediately after struct definition?
A struct Point { int x; int y; } p;
B struct Point { int x; int y; }; Point p;
C struct { int x; int y; } p;
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

Both methods are valid - A uses named struct tag with variable declaration, C uses anonymous struct with variable declaration.

Take 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.

Take Test
Consider union u { int i; char c; float f; }; What is the size of this union?
A 4 bytes
B 9 bytes
C 7 bytes
D Depends on compiler
Correct Answer:  A. 4 bytes
EXPLANATION

Union size equals the largest member size. float is typically 4 bytes, which is largest.

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