C Programming — File Handling
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in File Handling
Q.1 Hard File Handling
In a program that processes binary files with variable-length records, what could cause fread() to return fewer items than requested without reaching EOF?
A A read error occurred during I/O operation
B The file is opened in text mode instead of binary mode
C The buffer size is larger than remaining file size
D All 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.

Take Test
Q.2 Hard File Handling
For implementing a file locking mechanism in a competitive exam environment, which approach correctly handles concurrent file access in POSIX systems?
A Using flock() or fcntl() system calls with F_WRLCK
B Opening file in 'a' mode ensures automatic locking
C Using fseek() to check if file is accessible
D Creating 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.

Take Test
Q.3 Medium File Handling
What is the critical issue if ferror(fp) returns non-zero during file operations in a banking application?
A An error occurred during file I/O operation that should be handled
B The file pointer reached the end of file
C The file mode is incorrect
D Buffer 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()).

Take Test
Q.4 Medium File Handling
A competitive exam question requires processing a 500MB CSV file line by line. Which approach is most efficient in terms of memory?
A Read entire file into memory using fread(), then process
B Use fgets() with a fixed-size buffer for each line
C Use mmap() for memory-mapped file access
D Use 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.

Take Test
Q.5 Medium File Handling
In a data structure serialization scenario, what is returned by fwrite() on successful completion?
A The number of complete items successfully written
B The total number of bytes written
C Zero on success
D The 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.

Take Test
Advertisement
Q.6 Medium File Handling
What will be the output of fseek(fp, -10, SEEK_END) in a file of 100 bytes?
A File pointer moves to byte 90
B File pointer moves to byte 10
C Returns error because negative offset is invalid
D File 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.

Take Test
Q.7 Medium File Handling
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.

Take Test
Q.8 Easy File Handling
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
D There 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).

Take Test
Q.9 Easy File Handling
Which function is used to check if the end-of-file (EOF) has been reached in C file handling?
A feof(fp)
B eof(fp)
C iseof(fp)
D checkeof(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.

Take Test
Q.10 Hard File Handling
A program reads serialized objects using fread(). What's the primary risk when file format changes between versions?
A fread() will fail silently
B Buffer overflow or misaligned struct interpretation
C File size becomes invalid
D Automatic 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.

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