A large binary file of 10GB is being processed. Reading entire file into memory is impossible. Which approach is best?
Answer: A
fread(buffer, size, count, fp) efficiently reads file in manageable chunks. Process each chunk, then read next. This is standard practice for large file handling.
Q.142Hard
How can you determine the size of a file in bytes using standard C functions?
Answer: D
Multiple methods exist: fseek/ftell repositions and returns position, stat() gets file info including size, and fstat() does similar with file descriptor. All are valid.
Q.143Hard
When writing binary structures with fwrite(), which precaution is essential for portability across different systems?
Answer: A
Struct padding varies by architecture and compiler. Binary files written on one system may not read correctly on another due to different alignment. Use serialization techniques for portability.
Q.144Hard
A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
Answer: A
File I/O uses buffering for efficiency. Data in write buffer stays in memory until fflush(fp) explicitly flushes or fclose() is called, risking data loss if program crashes.
Q.145Hard
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
Answer: D
fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.
Advertisement
Q.146Hard
For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
Answer: A
fgets() is safest for line reading with size protection. For lines exceeding buffer size, use dynamic allocation or getline(). fscanf() with %s is vulnerable to buffer overflow.
Q.147Hard
What does rewind(fp) accomplish compared to fseek(fp, 0, SEEK_SET)?
Answer: B
rewind(fp) resets pointer AND clears EOF/error flags. fseek(fp, 0, SEEK_SET) only repositions. rewind is essentially: fseek(...); clearerr(...);
Q.148Hard
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
Answer: A
Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.
Q.149Hard
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
Answer: C
Fixed-size chunk processing is standard for large files. mmap() helps but may not be portable. Loading entire file exhausts memory. External tools bypass C programming.
Q.150Hard
When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
Answer: B
Standard C fopen/fwrite lack file locking. Multiple processes can corrupt data. Need OS-specific locking (flock/fcntl on Unix) or use 'a' mode with small writes.
Q.151Hard
A program reads serialized objects using fread(). What's the primary risk when file format changes between versions?
Answer: B
Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.
Q.152Hard
For implementing a file locking mechanism in a competitive exam environment, which approach correctly handles concurrent file access in POSIX systems?
Answer: A
flock() and fcntl() with F_WRLCK provide kernel-level file locking in POSIX systems. Option B is false, option C doesn't provide locking, and option D is unreliable due to race conditions.
Q.153Hard
In a program that processes binary files with variable-length records, what could cause fread() to return fewer items than requested without reaching EOF?
Answer: D
fread() can return fewer items due to: (A) I/O errors, (B) text mode conversions affecting byte count, or (C) insufficient data remaining. For robust code, check ferror() and feof() after fread() returns less than requested.
Q.154Hard
What will happen if malloc() fails and returns NULL but code doesn't check?
Answer: B
Dereferencing a NULL pointer causes undefined behavior, typically resulting in segmentation fault or program crash.
Q.155Hard
Which statement about dynamic memory is TRUE?
Answer: B
Dynamic memory persists until free() is called, unlike automatic variables which are freed at function end.
Q.156Hard
What is the issue in this code?
void func() {
int *p = malloc(sizeof(int));
*p = 5;
}
int main() {
func();
// p is not accessible here
return 0;
}
Answer: B
The pointer p is local to func(). Memory is allocated but never freed, causing a memory leak.
Q.157Hard
What is the correct way to allocate memory for 2D dynamic array (3x4)?
Answer: B
For true 2D dynamic array, allocate pointer array first, then allocate each row. Option C is 1D linear allocation.
Q.158Hard
What does this realloc() call do?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
Answer: B
realloc() resizes the memory block while preserving existing data. If expansion in-place fails, it allocates new block and copies data.
Q.159Hard
What is a common mistake in this code?
void func() {
int *ptr;
ptr = malloc(sizeof(int) * 5);
func2(ptr);
free(ptr);
}
void func2(int *p) {
free(p);
}
Answer: A
Memory is freed in func2(), then again in func(), causing double free error.
Q.160Hard
What is the risk in this code?
char *str = malloc(5);
strcpy(str, "Hello World");
Answer: D
Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.