In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
Answer: B
Dynamic allocation with getc() handles variable-length lines robustly. fgets() with fixed buffer may truncate. fscanf() with %s doesn't handle spaces. Option D wastes memory.
Q.762Medium
A file opened with 'r+b' mode has pointer at position 100. After fwrite(buffer, 1, 50, fp), the pointer is at:
Answer: B
After fwrite() of 50 bytes from position 100, pointer moves to 150. File pointer advances after read/write operations.
Q.763Medium
Which approach correctly implements a file copy function for both text and binary files?
Answer: D
Binary mode works for all files because it's just byte-for-byte copy. Text mode conversions are unnecessary for copying.
Q.764Medium
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
Answer: A
Not checking fopen() return value leads to NULL pointer dereference. Buffer overflow and permissions cause different errors. Disk space affects writing.
Q.765Medium
For a file with 1000 records, which approach minimizes I/O operations?
Answer: B
Single fread() for all records = 1 I/O operation. Loop-based approaches = 1000 operations. Fewer I/O calls = better performance.
Advertisement
Q.766Hard
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.767Hard
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.768Hard
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.769Hard
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.770Hard
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.771Easy
Which function is used to check if the end-of-file (EOF) has been reached in C file handling?
Answer: A
feof() is the standard C library function that returns non-zero if EOF has been reached on the given file stream. Other options are not valid C functions.
Q.772Easy
In a file processing application, what is the primary difference between 'wb' and 'w' file modes?
Answer: A
'w' opens file in text mode (converts line endings), while 'wb' opens in binary mode (no conversion). This matters when handling platform-specific line endings (\r\n vs \n).
Q.773Medium
A program needs to read and write to the same file alternately. Which file mode is most appropriate?
Answer: A
'r+' mode allows both reading and writing without truncating the file. 'w+' truncates the file on opening, 'a+' is for append operations. 'r+' preserves existing content while allowing modifications.
Q.774Medium
What will be the output of fseek(fp, -10, SEEK_END) in a file of 100 bytes?
Answer: A
fseek(fp, -10, SEEK_END) positions the pointer 10 bytes before the end of file. For a 100-byte file, this places it at position 90 (0-indexed). Negative offsets with SEEK_END are valid.
Q.775Medium
In a data structure serialization scenario, what is returned by fwrite() on successful completion?
Answer: A
fwrite(ptr, size, nmemb, fp) returns the number of complete items written, not bytes. If writing 10 items of 4 bytes each but only 35 bytes written, it returns 8, not 35.
Q.776Medium
A competitive exam question requires processing a 500MB CSV file line by line. Which approach is most efficient in terms of memory?
Answer: B
fgets() with a fixed buffer reads one line at a time, maintaining constant memory usage regardless of file size. Option A wastes memory, option D is slowest, and option C may cause issues with large files on some systems.
Q.777Medium
What is the critical issue if ferror(fp) returns non-zero during file operations in a banking application?
Answer: A
ferror() returns non-zero if an error has occurred in file operations. In critical applications like banking, this must be checked to ensure data integrity. This is different from EOF (checked by feof()).
Q.778Hard
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.779Hard
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.780Easy
Which header file is required for dynamic memory allocation functions?
Answer: B
The <stdlib.h> header file contains declarations for malloc(), calloc(), realloc(), and free() functions.