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.
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.
When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
Afopen() is too slow
BNo built-in file locking mechanism
Cfopen() doesn't support append atomically
DMemory leaks after multiple opens
Correct Answer:
B. No built-in file locking mechanism
EXPLANATION
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.
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
ARead entire file into heap-allocated buffer
BUse mmap() for memory-mapped I/O
CRead in fixed-size chunks in loop
DUse external sorting tools
Correct Answer:
C. Read in fixed-size chunks in loop
EXPLANATION
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.
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
AText mode converts line endings, corrupting binary data
BText mode stops at null bytes
CText mode is slower
DText mode reduces buffer size
Correct Answer:
A. Text mode converts line endings, corrupting binary data
EXPLANATION
Text mode on Windows converts CRLF to LF and vice versa, corrupting binary. Binary mode preserves exact bytes. Null byte handling is secondary concern.
For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
Afgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
Bfscanf(fp, "%s", buffer) with fixed-size array
Cfgetc() in a loop to manually read lines
Dfread() to read entire file at once
Correct Answer:
A. fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
EXPLANATION
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.
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
Afprintf() interprets format specifiers and may alter binary data representation
Bfprintf() is always safer than fwrite()
Cfprintf() cannot handle NULL characters in data
DBoth A and C are correct
Correct Answer:
D. Both A and C are correct
EXPLANATION
fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.
A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
AData written to buffer may not be physically written to disk until fflush() or fclose()
BFile pointer position becomes unreliable
CRead operations always return cached data from memory
DFile automatically truncates after buffer fills
Correct Answer:
A. Data written to buffer may not be physically written to disk until fflush() or fclose()
EXPLANATION
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.