C Programming — File Handling
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in File Handling
Q.11 Hard File Handling
When implementing a log file writer with concurrent access, what's the critical issue with standard fopen()?
A fopen() is too slow
B No built-in file locking mechanism
C fopen() doesn't support append atomically
D Memory 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.

Take Test
Q.12 Hard File Handling
For competitive exam file validation: A 2GB file must be processed without loading entirely in memory. Which strategy is optimal?
A Read entire file into heap-allocated buffer
B Use mmap() for memory-mapped I/O
C Read in fixed-size chunks in loop
D Use 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.

Take Test
Q.13 Hard File Handling
A program processes encrypted binary data with fread(). Why might using 'r' mode instead of 'rb' cause corruption?
A Text mode converts line endings, corrupting binary data
B Text mode stops at null bytes
C Text mode is slower
D Text 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.

Take Test
Q.14 Hard File Handling
What does rewind(fp) accomplish compared to fseek(fp, 0, SEEK_SET)?
A rewind() is faster
B rewind() also clears error indicators; fseek() doesn't
C They are functionally identical
D fseek() works only in binary mode
Correct Answer:  B. rewind() also clears error indicators; fseek() doesn't
EXPLANATION

rewind(fp) resets pointer AND clears EOF/error flags. fseek(fp, 0, SEEK_SET) only repositions. rewind is essentially: fseek(...); clearerr(...);

Take Test
Q.15 Medium File Handling
For a file with 1000 records, which approach minimizes I/O operations?
A Read one record at a time with fread()
B Read all records with single fread() call
C Use fgets() in loop for each record
D Both A and C are equivalent
Correct Answer:  B. Read all records with single fread() call
EXPLANATION

Single fread() for all records = 1 I/O operation. Loop-based approaches = 1000 operations. Fewer I/O calls = better performance.

Take Test
Advertisement
Q.16 Medium File Handling
A program crashes after reading a file. What's most likely the cause if error checking is minimal?
A File doesn't exist but fopen() wasn't checked
B Buffer overflow during fread()
C Insufficient disk space
D File 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.

Take Test
Q.17 Medium File Handling
Which approach correctly implements a file copy function for both text and binary files?
A Always use 'rb'/'wb' modes regardless of file type
B Detect file type and open accordingly
C Use 'r'/'w' for text, then reopen with 'rb'/'wb' for binary
D Binary copy is sufficient for all file types
Correct Answer:  D. Binary copy is sufficient for all file types
EXPLANATION

Binary mode works for all files because it's just byte-for-byte copy. Text mode conversions are unnecessary for copying.

Take Test
Q.18 Medium File Handling
A file opened with 'r+b' mode has pointer at position 100. After fwrite(buffer, 1, 50, fp), the pointer is at:
A 100
B 150
C 50
D Undefined position
Correct Answer:  B. 150
EXPLANATION

After fwrite() of 50 bytes from position 100, pointer moves to 150. File pointer advances after read/write operations.

Take Test
Q.19 Medium File Handling
In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
A Use fixed-size buffer with fgets()
B Use getc() in loop and dynamically allocate memory
C Use fscanf() with %s format
D Read 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.

Take Test
Q.20 Medium File Handling
Which statement correctly counts non-comment lines in a text file?
A Read each line and skip if first non-whitespace char is '//'
B Use grep to filter comments before processing
C Read line with fgets(), check for '//' pattern, increment counter if not found
D Use fscanf() with format specifier to skip comment lines
Correct Answer:  C. Read line with fgets(), check for '//' pattern, increment counter if not found
EXPLANATION

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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips