Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

198 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 71–80 of 198
Topics in C Programming
In a structure, what does the #pragma pack(1) directive do?
A Removes padding between members
B Adds padding between members
C Compresses structure data
D Aligns structure to word boundary
Correct Answer:  A. Removes padding between members
EXPLANATION

#pragma pack(1) removes padding, making members occupy consecutive memory locations for compact storage.

Take Test
What will be the output of this code?
struct X { int a; float b; };
struct Y { struct X x; int c; };
printf("%zu", sizeof(struct Y));
A 12
B 13
C 16
D Cannot determine
Correct Answer:  C. 16
EXPLANATION

Nested struct X: int(4) + float(4) = 8 bytes. Y: struct X(8) + padding(0) + int(4) = 12, but with overall alignment it becomes 16 bytes.

Take Test
In competitive programming, when should you use union instead of struct?
A When you need to store multiple values simultaneously
B When memory optimization and type reinterpretation are critical
C When you have more than 5 members
D Never - struct is always better
Correct Answer:  B. When memory optimization and type reinterpretation are critical
EXPLANATION

Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.

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

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

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

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

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

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