In a structure with flexible array members, which statement is INCORRECT?
struct FlexArray { int len; int arr[]; };
Answer: B
Flexible array members require dynamic allocation; the structure itself cannot be allocated on the stack with a defined size for the array.
Q.122Hard
What is the output of the following?
struct S { int a:3; int b:3; int c:3; };
printf("%zu", sizeof(struct S));
Answer: D
Bit field packing is implementation-defined. Size depends on compiler's bit field allocation strategy.
Q.123Hard
In competitive programming, when should you use union instead of struct?
Answer: B
Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.
Q.124Hard
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));
Answer: C
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.
Q.125Hard
In a structure, what does the #pragma pack(1) directive do?
Answer: A
#pragma pack(1) removes padding, making members occupy consecutive memory locations for compact storage.
Advertisement
Q.126Hard
If a structure contains a flexible array member, where must it be declared?
Answer: B
A flexible array member must be the last member of a structure. This is a C99 feature used for dynamic-sized arrays.
Q.127Hard
In competitive programming, when implementing a graph using adjacency lists, which data structure is most suitable?
Answer: B
An array of structures, where each structure contains a linked list (using self-referential pointers), is ideal for adjacency list representation.
Q.128Hard
If you have a structure with bit fields, which statement about their behavior is true?
Answer: A
Bit fields save memory by storing multiple values in single bytes, but behavior varies by compiler/platform, and you cannot take addresses of bit field members.
Q.129Hard
What is the output of this complex code?
struct S { char c; int i; } s;
printf("%lu", sizeof(s));
Answer: B
char(1 byte) + padding(3 bytes) + int(4 bytes) = 8 bytes due to alignment requirements.
Q.130Hard
In competitive programming, when you need a structure that can hold either an integer or a floating-point number (but not both simultaneously) in the most memory-efficient way, which should you use?
Answer: B
Union is memory-efficient as members share space. Only one can be accessed at a time, which suits the requirement.
Q.131Hard
What will be the output of the following code?
union Test {
int a;
char b;
};
union Test t;
t.a = 65;
printf("%d %c", t.a, t.b);
Answer: D
Since union members share memory, the output depends on the system's endianness. On little-endian systems, t.b would be 65 (or 'A'), but on big-endian systems, the result differs.
Q.132Hard
In competitive programming for GATE/ISRO 2025, which scenario would union be preferable over structure?
Answer: B
Unions are preferred when you have mutually exclusive data (only one member is used at a time) and memory is critical. A classic example is storing different data types in a tagged union pattern used in compilers and interpreters.
Q.133Hard
Write a code snippet to read 100 bytes from a file. Which is correct?
Answer: A
fread() syntax is fread(ptr, size, count, stream). To read 100 bytes, size=1 and count=100. Option B reads 100 items of 1 byte each, which is equivalent but less clear.
Q.134Hard
Which of the following correctly implements appending to a file?
Answer: A
Mode "a" opens a file for appending. New data is written at the end of the file. Mode "w" truncates the file, "r+" requires file to exist, "ab+" is appending in binary with both read/write.
Q.135Hard
In a program reading a binary file of 1000 integers, fread() is called as: fread(arr, sizeof(int), 1000, fp). If only 500 integers are present, what will fread() return?
Answer: A
fread() returns the number of complete items successfully read, not the number of bytes. It will return 500 even if 1000 were requested.
Q.136Hard
Consider using fseek() on a file opened in text mode with SEEK_END and a non-zero offset. What is the standard behavior?
Answer: B
In text mode, fseek() with non-zero offset relative to SEEK_END is undefined behavior. Use fseek(fp, 0, SEEK_END) for reliable behavior.
Q.137Hard
A program writes data using fprintf() and later attempts to read it back. However, the read operation fails intermittently. What could be the most likely cause?
Answer: B
fprintf() buffers output. Without flushing (fflush() or fclose()), subsequent read operations may see incomplete data. This is a common source of bugs.
Q.138Hard
What is the purpose of using fflush() in file handling, and when is it critical to use it?
Answer: B
fflush() writes any buffered data to the underlying file. It's critical when you need to ensure data is written to disk before continuing, especially before reading the same file.
Q.139Hard
A program writes binary data using fwrite() but reads it back with fprintf(). What will happen?
Answer: B
Mixing write (fwrite - binary) and read (fprintf - formatted text) functions on same data causes mismatch. Binary data won't have format specifiers; misinterpretation results.
Q.140Hard
A program uses fgetc() to read 100,000 characters from a file sequentially. Which alternative would be more efficient?
Answer: B
fread() with buffer reads multiple bytes per call, reducing function call overhead. fgetc() makes 100,000 separate calls. fgets() is limited to reading up to newline.