File Input and Output # MCQs Practice set

Q.1 Which mode should be used in C to open a file for reading only?

r
w
a
rw
Explanation - The 'r' mode opens a file for reading. If the file does not exist, fopen() returns NULL.
Correct answer is: r

Q.2 Which function is used to write a string to a file in C?

fprintf()
fputs()
fscanf()
fgets()
Explanation - fputs() writes a string to the file, excluding the null character '\0'.
Correct answer is: fputs()

Q.3 What will fopen() return if a file cannot be opened?

0
NULL
-1
EOF
Explanation - fopen() returns NULL if it fails to open a file due to reasons like non-existent file in read mode or lack of permissions.
Correct answer is: NULL

Q.4 Which function is used to read a single character from a file in C?

fgetc()
fputc()
getc()
getchar()
Explanation - fgetc() reads the next character from the specified file stream.
Correct answer is: fgetc()

Q.5 Which of the following modes opens a file for both reading and writing?

r+
w+
a+
All of the above
Explanation - 'r+', 'w+', and 'a+' all allow reading and writing, but differ in how they handle existing data and file creation.
Correct answer is: All of the above

Q.6 Which function is used to move the file pointer to a specific location in a file?

fseek()
ftell()
rewind()
fread()
Explanation - fseek() moves the file pointer to a specified offset from a given origin (beginning, current, or end).
Correct answer is: fseek()

Q.7 Which function returns the current position of the file pointer?

fseek()
ftell()
rewind()
fclose()
Explanation - ftell() returns the current position of the file pointer as a long integer.
Correct answer is: ftell()

Q.8 What does the 'a' mode in fopen() do?

Opens a file for reading
Appends data to the end of file
Overwrites the file
Opens file in binary mode
Explanation - Mode 'a' opens the file for writing at the end. If the file does not exist, it creates a new file.
Correct answer is: Appends data to the end of file

Q.9 Which function is used to read formatted data from a file in C?

fscanf()
fprintf()
fgets()
fputc()
Explanation - fscanf() reads data from a file according to a specified format.
Correct answer is: fscanf()

Q.10 What is the correct way to close a file in C?

close(file)
fclose(file)
file.close()
endfile(file)
Explanation - fclose() closes the file associated with the file pointer and frees resources.
Correct answer is: fclose(file)

Q.11 Which function reads a line of text from a file?

fgets()
fputs()
fscanf()
fprintf()
Explanation - fgets() reads characters from a file until a newline is encountered or the specified number of characters is read.
Correct answer is: fgets()

Q.12 Which function writes a single character to a file in C?

fgetc()
fputc()
putc()
fprintf()
Explanation - fputc() writes a single character to the specified file stream.
Correct answer is: fputc()

Q.13 In binary mode, which character represents end-of-file?

EOF
NULL
'\0'
None of these
Explanation - EOF is returned by input functions when the end of file is reached in binary or text mode.
Correct answer is: EOF

Q.14 Which of the following will overwrite the contents of a file if it exists?

w
a
r
r+
Explanation - Mode 'w' opens a file for writing. If the file exists, its contents are truncated.
Correct answer is: w

Q.15 Which function sets the file position to the beginning of the file?

fseek()
rewind()
ftell()
fclose()
Explanation - rewind() moves the file pointer to the beginning of the file.
Correct answer is: rewind()

Q.16 Which function can be used to determine the size of a file in C?

ftell() and fseek()
fgets()
fgetc()
fscanf()
Explanation - By moving the file pointer to the end using fseek() and then using ftell(), you can get the file size.
Correct answer is: ftell() and fseek()

Q.17 Which mode opens a file for reading and writing but does not truncate it if it exists?

r+
w+
a+
w
Explanation - 'r+' allows reading and writing to a file without truncating its existing content.
Correct answer is: r+

Q.18 Which function writes formatted data to a file?

fprintf()
fscanf()
fputc()
fgets()
Explanation - fprintf() writes data to a file according to a specified format string.
Correct answer is: fprintf()

Q.19 What does the 'b' in 'rb' or 'wb' stand for?

binary
buffered
both
beginning
Explanation - The 'b' character in mode string opens a file in binary mode instead of text mode.
Correct answer is: binary

Q.20 Which function reads data from a file into memory?

fread()
fwrite()
fputs()
fgets()
Explanation - fread() reads a specified number of bytes from a file into a buffer.
Correct answer is: fread()

Q.21 Which function writes data from memory to a file?

fwrite()
fread()
fputs()
fprintf()
Explanation - fwrite() writes a specified number of bytes from a buffer to a file.
Correct answer is: fwrite()

Q.22 Which of the following is true about text files in C?

They store data as a sequence of characters
They store data in binary format
They cannot be read
They require special libraries
Explanation - Text files store data as readable characters, unlike binary files.
Correct answer is: They store data as a sequence of characters

Q.23 Which function can be used to check for end-of-file while reading?

feof()
ferror()
fseek()
ftell()
Explanation - feof() returns a non-zero value if the end of the file has been reached.
Correct answer is: feof()

Q.24 Which of the following statements about 'a+' mode is correct?

File is opened for appending and reading
File is truncated if it exists
File can only be read
File can only be written
Explanation - 'a+' opens a file for both reading and appending at the end.
Correct answer is: File is opened for appending and reading

Q.25 Which of the following is true about binary files?

They can store images and audio
They store data as characters
They are only for text
They are not supported in C
Explanation - Binary files store data in raw format and can include non-text data like images, audio, or executable code.
Correct answer is: They can store images and audio