File Handling # MCQs Practice set

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

w
r
a
x
Explanation - The mode 'r' is used to open a file in read-only mode in Python.
Correct answer is: r

Q.2 In C, which function is used to open a file?

fread()
fopen()
open()
file()
Explanation - The fopen() function is used in C to open files with the desired mode.
Correct answer is: fopen()

Q.3 Which file mode in Python is used to append data at the end of a file?

a
w
r
x
Explanation - The 'a' mode appends data at the end of the file without overwriting.
Correct answer is: a

Q.4 What does the 'wb' mode mean in Python file handling?

Write Binary
Write Back
Write Buffered
Wrong Binary
Explanation - 'wb' is used to open a file in write mode for binary data.
Correct answer is: Write Binary

Q.5 Which of the following closes an opened file in C?

fileclose()
fclose()
endfile()
closefile()
Explanation - The fclose() function is used to close a file in C.
Correct answer is: fclose()

Q.6 In Python, which method reads the entire content of a file?

read()
readline()
readlines()
open()
Explanation - The read() method reads the complete file content as a single string.
Correct answer is: read()

Q.7 Which of these modes is NOT valid in Python file handling?

r+
w+
rw
a+
Explanation - 'rw' is not a valid mode. Valid combined modes are r+, w+, and a+.
Correct answer is: rw

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

fprintf()
fputs()
putfile()
writestring()
Explanation - The fputs() function writes a string to a file in C.
Correct answer is: fputs()

Q.9 What is the default file mode in Python's open() function?

w
r
a
rb
Explanation - By default, open() opens a file in 'read' mode.
Correct answer is: r

Q.10 Which method reads one line from a file in Python?

readline()
read()
line()
getline()
Explanation - The readline() method reads a single line from the file.
Correct answer is: readline()

Q.11 In C, which function is used to read formatted input from a file?

fread()
fgetc()
fscanf()
fgets()
Explanation - fscanf() is used for formatted input, similar to scanf but from a file.
Correct answer is: fscanf()

Q.12 What will happen if you open a file with mode 'w' in Python?

File is opened for reading only
File is opened for appending
File is created or truncated for writing
File cannot be opened
Explanation - Mode 'w' overwrites an existing file or creates a new one if it doesn’t exist.
Correct answer is: File is created or truncated for writing

Q.13 Which C function is used to move the file pointer to a specific location?

fseek()
ftell()
rewind()
goto()
Explanation - fseek() allows movement of the file pointer to a desired position.
Correct answer is: fseek()

Q.14 In Python, which statement is recommended for automatic file closing?

close()
with
end
exit
Explanation - The 'with' statement ensures proper file closing after use.
Correct answer is: with

Q.15 What does EOF stand for in file handling?

End of File
Error on File
Extra Open File
End or Finish
Explanation - EOF is a condition indicating the end of a file has been reached.
Correct answer is: End of File

Q.16 Which Python function checks if a file exists?

os.path.isfile()
file.exists()
checkfile()
exists()
Explanation - The os.path.isfile() function is used to check if a file exists.
Correct answer is: os.path.isfile()

Q.17 Which of the following is true about text and binary modes?

Text mode stores data in binary format
Binary mode reads/writes raw bytes
Both are the same
Binary mode is default
Explanation - Binary mode deals with raw bytes while text mode handles encoded text.
Correct answer is: Binary mode reads/writes raw bytes

Q.18 Which method is used to write a list of strings to a file in Python?

writelines()
write()
append()
putlines()
Explanation - The writelines() method writes multiple strings (e.g., from a list) to a file.
Correct answer is: writelines()

Q.19 In C, which function gives the current position of the file pointer?

fseek()
ftell()
rewind()
position()
Explanation - The ftell() function returns the current file pointer location.
Correct answer is: ftell()

Q.20 Which file mode will throw an error if the file already exists in Python?

x
w
r
a
Explanation - The 'x' mode is used to create a file but raises an error if it already exists.
Correct answer is: x

Q.21 What is the function of rewind() in C?

Close file
Move pointer to beginning
Delete file
Append file
Explanation - rewind() resets the file pointer to the start of the file.
Correct answer is: Move pointer to beginning

Q.22 Which Python method reads all lines of a file into a list?

read()
readline()
readlines()
lines()
Explanation - The readlines() method stores all lines of a file as list elements.
Correct answer is: readlines()

Q.23 In C, which function is used to read a single character from a file?

fgetc()
getc()
getchar()
fread()
Explanation - The fgetc() function reads a single character from a file.
Correct answer is: fgetc()

Q.24 Which of the following functions in Python can be used to delete a file?

os.remove()
delete()
removefile()
unlinkfile()
Explanation - The os.remove() function deletes a file from the system.
Correct answer is: os.remove()

Q.25 In file handling, what does buffering help with?

Faster I/O operations
Slow processing
Security
Error handling
Explanation - Buffering stores data temporarily in memory for efficient I/O operations.
Correct answer is: Faster I/O operations