What is the critical issue if ferror(fp) returns non-zero during file operations in a banking application?
AAn error occurred during file I/O operation that should be handled
BThe file pointer reached the end of file
CThe file mode is incorrect
DBuffer overflow is detected
Correct Answer:
A. An error occurred during file I/O operation that should be handled
EXPLANATION
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()).
A competitive exam question requires processing a 500MB CSV file line by line. Which approach is most efficient in terms of memory?
ARead entire file into memory using fread(), then process
BUse fgets() with a fixed-size buffer for each line
CUse mmap() for memory-mapped file access
DUse getc() to read one character at a time
Correct Answer:
B. Use fgets() with a fixed-size buffer for each line
EXPLANATION
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.
In a data structure serialization scenario, what is returned by fwrite() on successful completion?
AThe number of complete items successfully written
BThe total number of bytes written
CZero on success
DThe position of file pointer after writing
Correct Answer:
A. The number of complete items successfully written
EXPLANATION
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.
What will be the output of fseek(fp, -10, SEEK_END) in a file of 100 bytes?
AFile pointer moves to byte 90
BFile pointer moves to byte 10
CReturns error because negative offset is invalid
DFile pointer remains unchanged
Correct Answer:
A. File pointer moves to byte 90
EXPLANATION
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.
A program needs to read and write to the same file alternately. Which file mode is most appropriate?
A'r+'
B'w+'
C'a+'
D'rb+'
Correct Answer:
A. 'r+'
EXPLANATION
'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.
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
AFile doesn't exist but fopen() wasn't checked
BBuffer overflow during fread()
CInsufficient disk space
DFile permissions are read-only
Correct Answer:
A. File doesn't exist but fopen() wasn't checked
EXPLANATION
Not checking fopen() return value leads to NULL pointer dereference. Buffer overflow and permissions cause different errors. Disk space affects writing.
In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
AUse fixed-size buffer with fgets()
BUse getc() in loop and dynamically allocate memory
CUse fscanf() with %s format
DRead entire file into memory at once
Correct Answer:
B. Use getc() in loop and dynamically allocate memory
EXPLANATION
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.