Which function is used to read a string from a file, including whitespace characters, up to a maximum length?
Answer: B
fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.
Q.22Medium
In the context of ferror() function, what does it return?
Answer: A
ferror() returns a non-zero value if an error flag is set for the stream, otherwise returns 0.
Q.23Medium
Which mode should be used if you want to read and write to a file without truncating existing content?
Answer: B
The 'r+' mode opens a file for both reading and writing without truncating it. The file must exist.
Q.24Medium
What is the purpose of the clearerr() function in file handling?
Answer: A
clearerr() clears both the EOF indicator and the error flag associated with a stream.
Q.25Medium
Consider a scenario where fopen() fails to open a file. Which of the following is the correct way to handle it?
Answer: C
fopen() returns NULL pointer on failure. Checking 'fp == NULL' or '!fp' is the correct approach. Option C uses explicit NULL check which is clearer.
Advertisement
Q.26Medium
What does ftell() function return?
Answer: B
ftell() returns the current position of the file pointer as a long integer offset from the beginning of the file.
Q.27Medium
Which of the following fseek() calls will position the file pointer at the end of the file?
Answer: C
fseek(fp, 0, SEEK_END) positions the file pointer at the end of file. SEEK_END is the third constant in <stdio.h>.
Q.28Medium
What is the difference between text mode and binary mode when reading files?
Answer: B
In text mode, platform-specific line endings (\r\n on Windows, \n on Unix) are translated. Binary mode reads bytes as-is without translation.
Q.29Medium
Which function is used to read formatted data from a file, similar to scanf() but for files?
Answer: C
fscanf() reads formatted data from a file stream, functioning as the file equivalent of scanf().
Q.30Medium
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.31Hard
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.32Hard
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.33Hard
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.
Q.34Hard
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.35Easy
Which function is used to determine the current position in a file?
Answer: A
ftell() returns the current position (as a long integer) of the file pointer in the file. fseek() is used to change position, rewind() resets to beginning, and fgetpos() stores position in a structure.
Q.36Easy
What is the correct syntax to open a file named 'data.txt' in read mode?
Answer: B
fopen() uses double quotes for string arguments in C. The first argument is filename and second is mode. Single quotes are for characters, not strings.
Q.37Easy
Which mode allows both reading and writing to a file, creating it if it doesn't exist?
Answer: B
"w+" opens file for reading and writing, truncating it if it exists, and creating it if it doesn't. "r+" requires file to exist. "a+" opens in append mode with read capability.
Q.38Easy
What does the fclose() function do?
Answer: B
fclose() terminates the file stream, flushes any buffered data to disk, and releases the file handle. It doesn't delete or clear the file.
Q.39Medium
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.40Easy
Which function writes formatted output to a file?
Answer: B
fprintf() writes formatted data to file (like printf() but to file). fscanf() reads formatted data. fwrite() writes binary data. fputs() writes strings.