Home Subjects C Programming Structures & Unions

C Programming
Structures & Unions

C language from basics to advanced placement prep

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

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.

Test
What is the relationship between structure alignment and padding in modern C compilers?
A They are unrelated concepts
B Padding is added to achieve proper alignment based on system architecture
C Alignment only applies to unions
D Padding reduces alignment efficiency
Correct Answer:  B. Padding is added to achieve proper alignment based on system architecture
EXPLANATION

Compilers add padding bytes between structure members to align them on boundaries (usually power of 2), improving memory access performance on the target architecture.

Test
In nested structures with pointers, which access method is correct? struct outer { struct inner *ptr; } *o; Accessing inner's member x:
A o->ptr->x
B (*o).ptr->x
C o->(*ptr).x
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

Both o->ptr->x and (*o).ptr->x are equivalent. Arrow operator can chain for pointers, and (*pointer).member is equivalent to pointer->member.

Test
Which approach is more memory efficient for storing 100 flags: array of char or bit fields in a structure?
A Array of char (100 bytes)
B Bit fields (13 bytes approximately)
C Both use same memory
D Array is always better
Correct Answer:  B. Bit fields (13 bytes approximately)
EXPLANATION

Bit fields can pack 8 flags per byte, so 100 flags need ~13 bytes. Char array needs 100 bytes. Bit fields are more memory-efficient for flag storage.

Test
Given union test { int x; char y[4]; }; If you set y[0]=65, y[1]=66, what happens to x?
A x remains 0
B x becomes unpredictable
C x gets modified based on byte representation
D x automatically converts to string
Correct Answer:  C. x gets modified based on byte representation
EXPLANATION

Since union members share memory, setting y[0]=65 and y[1]=66 overwrites the int x value. The exact value depends on endianness.

Test
What is the difference between self-referential and recursive structures?
A They are the same thing
B Self-referential contains pointer to same type, recursive is function-based
C Recursive structures are not allowed in C
D Self-referential requires extern keyword
Correct Answer:  B. Self-referential contains pointer to same type, recursive is function-based
EXPLANATION

Self-referential structures contain pointers to the same structure type (like linked list nodes). Recursion refers to function calls, not structures.

Test
How can you initialize a structure array of 10 elements partially in C?
A struct emp arr[10] = {0};
B struct emp arr[10] = {};
C struct emp arr[10] = {NULL};
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

Both {0} and {} will initialize all members to zero. These are equivalent in C99 and later standards.

Test
What happens if you assign a union member and then access another member?
struct u { int a; char b; }; u.a = 257; printf("%d", u.b);
A Prints 257
B Prints 1 (LSB of 257)
C Prints garbage
D Compilation error
Correct Answer:  B. Prints 1 (LSB of 257)
EXPLANATION

In a union, both members share memory. 257 in binary is 100000001. u.b (char) reads only the LSB, which is 1.

Test
Which statement is true about nested structures in C?
A Nested structures are not allowed
B A structure can contain another structure as a member
C Nested structures require pointers
D Nested structures can only be used with unions
Correct Answer:  B. A structure can contain another structure as a member
EXPLANATION

C allows structures to have other structures as members. This is called nested structures and is a common practice.

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