What will be the behavior of fgets() when reading from an empty line in a file?
Answer: B
fgets() reads up to and including the newline character. For an empty line, it reads just the newline character into the buffer.
Q.22Medium
A program reads a text file line by line using fgets(). If a line contains more characters than the buffer size, what happens?
Answer: A
fgets(buffer, size, fp) reads at most size-1 characters or until newline, whichever comes first, then adds null terminator. Remaining characters stay in stream for next read.
Q.23Medium
A file is opened in binary mode. How will fseek() behave differently compared to text mode?
Answer: C
In text mode, fseek() behavior is implementation-dependent due to line-ending conversions (\r\n vs \n). Binary mode provides more predictable fseek() behavior with absolute byte positions.
Q.24Medium
A program uses fprintf() to write 1000 integers to a file but doesn't call fclose(). What could happen?
Answer: B
Without fclose() or fflush(), buffered data remains in memory. If program terminates, that data never reaches disk. System may flush on exit but it's unreliable.
Q.25Medium
A binary file contains 500 structures of size 32 bytes each. To skip the first 10 structures and read the 11th one, which fseek() call is correct?
Answer: B
To skip first 10 structures and read 11th: 10 * 32 = 320 bytes from start. Option B (320 bytes) positions pointer at start of 11th structure (320 bytes offset).
Advertisement
Q.26Medium
Which of the following modes will truncate an existing file?
Answer: C
"w" mode opens file for writing and truncates it to zero length if it exists. "r+" doesn't truncate. "a" and "a+" are append modes that don't truncate.
Q.27Medium
A program reads a CSV file with fgets() storing data in a 256-byte buffer. Some lines exceed 255 characters. How should this be handled?
Answer: A
Best solution is allocate larger buffer. If line is 500 chars, use buffer >= 501. Option C partially works but complicates newline handling. fscanf() doesn't automatically handle long lines.
Q.28Medium
What does fflush(fp) do when fp is a file pointer?
Answer: B
fflush(fp) forces buffered output to be written to disk immediately without closing the file. Useful for ensuring data safety before risky operations.
Q.29Medium
In a file opened in "a+" mode, where does the file pointer initially point?
Answer: B
In append mode "a" or "a+", file pointer initially points to end of file for writing. For reading in "a+", you must explicitly seek to read from start.
Q.30Medium
Which mode allows both reading and appending without truncating existing content?
Answer: A
'a+' mode opens file for reading and appending. New data is added at the end without removing existing content. 'w+' would truncate the file.
Q.31Medium
What does the function ftell(fp) return in file handling?
Answer: A
ftell(fp) returns the current position of the file pointer (in bytes) from the beginning of the file. Returns -1L on error.
Q.32Medium
Which of the following correctly reads a string from file with size limit?
Answer: A
fgets(buffer, size, fp) safely reads up to (size-1) characters or until newline from file fp, preventing buffer overflow. fscanf() is vulnerable to overflow.
Q.33Medium
A program needs to update specific bytes in the middle of a file without affecting other data. Which approach is most suitable?
Answer: A
Using fseek() with r+ or w+ mode allows direct access to specific file positions. This is efficient for small modifications without rewriting the entire file.
Q.34Medium
What is the difference between 'rb' and 'r' modes when opening a file?
Answer: A
'rb' (binary read) reads data as-is without converting line endings. 'r' (text read) converts platform-specific newlines (\r\n on Windows) to \n.
Q.35Medium
In a file opened with 'rb+' mode, what operations can be performed?
Answer: A
'rb+' opens a binary file for both reading and writing. The file must exist. Existing content is preserved and can be overwritten at any position.
Q.36Medium
A program reads structured data: fread(ptr, sizeof(struct), count, fp). If fread() returns a value less than 'count', what could be the reason?
Answer: A
fread() returns the number of items actually read. If less than 'count' is returned, it indicates EOF was reached or a read error occurred before completing the request.
Q.37Medium
In competitive exam file handling questions, if fseek() is called with SEEK_CUR, what does it indicate?
Answer: A
SEEK_CUR is a constant meaning 'current position'. fseek(fp, offset, SEEK_CUR) moves the pointer by 'offset' bytes from its current position.
Q.38Medium
A C program must securely close a file and handle potential errors. What is the best approach?
Answer: A
fclose() returns 0 on success and EOF (-1) on error. Checking return value ensures buffered data is flushed and handles errors like write failures.
Q.39Medium
A program needs to append 500 integers to an existing binary file. Which combination is correct?
Answer: A
Binary append requires 'ab' mode with fwrite(). Option B uses text mode. Option D would overwrite. Option C mixes binary mode with text function.
Q.40Medium
What happens if you call fclose() on an already closed file pointer?
Answer: B
Calling fclose() on closed file causes undefined behavior and returns EOF. This is a runtime issue, not compile-time.