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 61–70 of 198
Topics in C Programming
Q.61 Hard File Handling
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?
A 500
B An error code
C NULL
D The bytes read (typically 2000)
Correct Answer:  A. 500
EXPLANATION

fread() returns the number of complete items successfully read, not the number of bytes. It will return 500 even if 1000 were requested.

Take Test
Q.62 Hard File Handling
Which of the following correctly implements appending to a file?
A FILE *fp = fopen("file.txt", "a");
B FILE *fp = fopen("file.txt", "w");
C FILE *fp = fopen("file.txt", "r+");
D FILE *fp = fopen("file.txt", "ab+");
Correct Answer:  A. FILE *fp = fopen("file.txt", "a");
EXPLANATION

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.

Take Test
Q.63 Hard File Handling
Write a code snippet to read 100 bytes from a file. Which is correct?
A fread(buffer, 1, 100, fp);
B fread(buffer, 100, 1, fp);
C fread(buffer, 100, fp);
D fread(100, buffer, fp);
Correct Answer:  A. fread(buffer, 1, 100, fp);
EXPLANATION

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.

Take Test
In competitive programming for GATE/ISRO 2025, which scenario would union be preferable over structure?
A When you need to store multiple data types independently
B When memory optimization is critical and you need mutually exclusive data
C When creating complex data structures like trees
D When implementing object-oriented features
Correct Answer:  B. When memory optimization is critical and you need mutually exclusive data
EXPLANATION

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.

Take Test
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);
A 65 A
B 65 65
C 0 A
D Depends on endianness
Correct Answer:  D. Depends on endianness
EXPLANATION

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.

Take Test
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?
A struct with both members
B union with both members
C An array of size 2
D A single variable with type casting
Correct Answer:  B. union with both members
EXPLANATION

Union is memory-efficient as members share space. Only one can be accessed at a time, which suits the requirement.

Take Test
What is the output of this complex code?
struct S { char c; int i; } s;
printf("%lu", sizeof(s));
A 5
B 8
C 6
D 4
Correct Answer:  B. 8
EXPLANATION

char(1 byte) + padding(3 bytes) + int(4 bytes) = 8 bytes due to alignment requirements.

Take Test
If you have a structure with bit fields, which statement about their behavior is true?
A Bit fields can improve memory efficiency for small values
B Bit fields guarantee portability across all platforms
C Bit fields allow taking addresses (&) of members
D Bit fields support all data types
Correct Answer:  A. Bit fields can improve memory efficiency for small values
EXPLANATION

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.

Take Test
In competitive programming, when implementing a graph using adjacency lists, which data structure is most suitable?
A Array of unions
B Array of structures containing linked lists
C Single structure
D Union of arrays
Correct Answer:  B. Array of structures containing linked lists
EXPLANATION

An array of structures, where each structure contains a linked list (using self-referential pointers), is ideal for adjacency list representation.

Take Test
If a structure contains a flexible array member, where must it be declared?
A At the beginning of the structure
B At the end of the structure
C Anywhere in the structure
D In a separate union
Correct Answer:  B. At the end of the structure
EXPLANATION

A flexible array member must be the last member of a structure. This is a C99 feature used for dynamic-sized arrays.

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