fopen() is the standard C library function used to open a file. It takes two arguments: filename and mode.
Q.2Easy
What does the mode 'rb' mean in fopen()?
Answer: A
'rb' mode opens a file for reading in binary format. Binary mode reads the exact bytes without any text conversion.
Q.3Easy
Which function is used to close a file in C?
Answer: A
fclose() is the standard function to close an opened file stream. It returns 0 on success and EOF on error.
Q.4Easy
Which mode opens a file for writing and truncates it if it exists?
Answer: A
"w" mode opens a file for writing. If the file exists, it truncates the file to zero length. If it doesn't exist, a new file is created.
Q.5Easy
When opening a file in 'w' mode, what happens if the file already exists?
Answer: A
Opening a file in 'w' mode truncates the file to zero length if it exists, discarding all previous content.
Advertisement
Q.6Easy
Which of the following file modes in fopen() allows reading only?
Answer: B
The 'r' mode opens a file for reading only. The file must exist, otherwise fopen() returns NULL.
Q.7Easy
In file handling, what does the 'b' flag in file modes like 'rb' or 'wb' indicate?
Answer: A
The 'b' flag specifies binary mode, which prevents any translation of line endings or special characters.
Q.8Easy
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.9Easy
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.10Easy
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.11Easy
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.12Easy
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.
Q.13Easy
What will fseek(fp, 0, SEEK_END) do?
Answer: B
SEEK_END moves pointer relative to end of file. Offset 0 means go to the exact end. SEEK_SET (beginning) and SEEK_CUR (current) are other origin options.
Q.14Easy
Which function reads formatted data from a file?
Answer: B
fscanf() reads formatted data from file (like scanf() but from file). fgets() reads strings. fread() reads binary data. getc() reads single character.
Q.15Easy
What is returned by fopen() if file opening fails?
Answer: B
fopen() returns NULL pointer on failure. Proper practice is to check: if(fp == NULL) {...error handling...}. Returning -1 is for system calls like open().
Q.16Easy
What does the third parameter of fseek() represent?
Answer: B
Third parameter is 'whence': SEEK_SET (beginning), SEEK_CUR (current position), SEEK_END (end). Second parameter specifies offset in bytes.
Q.17Easy
Which header file must be included to use file handling functions in C?
Answer: A
stdio.h (Standard Input/Output) contains all file handling functions like fopen(), fclose(), fread(), fwrite(), etc.
Q.18Easy
What does the 'w+' mode do when opening a file with fopen()?
Answer: A
'w+' mode opens a file for both reading and writing. If the file doesn't exist, it creates a new one. If it exists, it truncates the file to zero length.
Q.19Easy
Which function is used to write a single character to a file?
Answer: A
fputc() writes a single character to the specified file. fgetc() reads a character, fwrite() writes blocks, and fprintf() writes formatted output.
Q.20Easy
What is the return value of feof(fp) when the end of file is reached?
Answer: A
feof(fp) returns a non-zero value when EOF (End Of File) is reached, and returns zero when not at EOF.