Write a code snippet to read 100 bytes from a file. Which is correct?
Answer: A
fread() syntax is fread(ptr, size, count, stream). To read 100 bytes, size=1 and count=100. Option B reads 100 items of 1 byte each, which is equivalent but less clear.
Q.2Hard
Which of the following correctly implements appending to a file?
Answer: A
Mode "a" opens a file for appending. New data is written at the end of the file. Mode "w" truncates the file, "r+" requires file to exist, "ab+" is appending in binary with both read/write.
Q.3Hard
In a program reading a binary file of 1000 integers, fread() is called as: fread(arr, sizeof(int), 1000, fp). If only 500 integers are present, what will fread() return?
Answer: A
fread() returns the number of complete items successfully read, not the number of bytes. It will return 500 even if 1000 were requested.
Q.4Hard
Consider using fseek() on a file opened in text mode with SEEK_END and a non-zero offset. What is the standard behavior?
Answer: B
In text mode, fseek() with non-zero offset relative to SEEK_END is undefined behavior. Use fseek(fp, 0, SEEK_END) for reliable behavior.
Q.5Hard
A program writes data using fprintf() and later attempts to read it back. However, the read operation fails intermittently. What could be the most likely cause?
Answer: B
fprintf() buffers output. Without flushing (fflush() or fclose()), subsequent read operations may see incomplete data. This is a common source of bugs.
Advertisement
Q.6Hard
What is the purpose of using fflush() in file handling, and when is it critical to use it?
Answer: B
fflush() writes any buffered data to the underlying file. It's critical when you need to ensure data is written to disk before continuing, especially before reading the same file.
Q.7Hard
A program writes binary data using fwrite() but reads it back with fprintf(). What will happen?
Answer: B
Mixing write (fwrite - binary) and read (fprintf - formatted text) functions on same data causes mismatch. Binary data won't have format specifiers; misinterpretation results.
Q.8Hard
A program uses fgetc() to read 100,000 characters from a file sequentially. Which alternative would be more efficient?
Answer: B
fread() with buffer reads multiple bytes per call, reducing function call overhead. fgetc() makes 100,000 separate calls. fgets() is limited to reading up to newline.
Q.9Hard
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.10Hard
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.11Hard
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.12Hard
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.13Hard
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.
Q.14Hard
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.15Hard
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.16Hard
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.17Hard
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.18Hard
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.19Hard
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.20Hard
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.