In a program that processes binary files with variable-length records, what could cause fread() to return fewer items than requested without reaching EOF?
AA read error occurred during I/O operation
BThe file is opened in text mode instead of binary mode
CThe buffer size is larger than remaining file size
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
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.
For implementing a file locking mechanism in a competitive exam environment, which approach correctly handles concurrent file access in POSIX systems?
AUsing flock() or fcntl() system calls with F_WRLCK
BOpening file in 'a' mode ensures automatic locking
CUsing fseek() to check if file is accessible
DCreating a separate lock file and checking its existence
Correct Answer:
A. Using flock() or fcntl() system calls with F_WRLCK
EXPLANATION
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.
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.
In a file processing application, what is the primary difference between 'wb' and 'w' file modes?
A'wb' opens in binary mode while 'w' opens in text mode
B'w' is faster than 'wb'
C'wb' allows reading while 'w' does not
DThere is no difference in modern C compilers
Correct Answer:
A. 'wb' opens in binary mode while 'w' opens in text mode
EXPLANATION
'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).
Which function is used to check if the end-of-file (EOF) has been reached in C file handling?
Afeof(fp)
Beof(fp)
Ciseof(fp)
Dcheckeof(fp)
Correct Answer:
A. feof(fp)
EXPLANATION
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.
A program reads serialized objects using fread(). What's the primary risk when file format changes between versions?
Afread() will fail silently
BBuffer overflow or misaligned struct interpretation
CFile size becomes invalid
DAutomatic version detection prevents issues
Correct Answer:
B. Buffer overflow or misaligned struct interpretation
EXPLANATION
Changed file format causes struct misalignment, leading to buffer overflow or garbage data. No automatic detection in C. Need version headers or checksums.