A program reads struct records using fread(). After reading 100 records of 64 bytes each, what is the total bytes read if successful?
Answer: C
fread(ptr, 64, 100, fp) reads 100 items of 64 bytes each = 6400 bytes total. fread() returns count of items read, not bytes.
Q.42Medium
Which statement correctly counts non-comment lines in a text file?
Answer: C
Pure C solution uses fgets() to read lines and strstr() or manual checking for comment patterns. Option B uses external tool. Option D is inefficient.
Q.43Medium
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.44Medium
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.45Medium
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.
Advertisement
Q.46Medium
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.47Medium
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.
Q.48Medium
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.49Medium
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.50Medium
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.51Medium
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.52Medium
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()).