Which of the following modes opens a file for both reading and writing without truncating the file, and raises an error if the file does not exist?
Answer: B
The mode 'r+' opens an existing file for both reading and writing. It raises FileNotFoundError if the file does not exist and does not truncate the file. 'w+' truncates the file (or creates it), 'a+' always appends, and 'x+' creates a new file but fails if it already exists.
Q.2Medium
What will be the output of the following code?
with open('test.txt', 'w') as f:
f.write('Hello')
f.write(' World')
with open('test.txt', 'r') as f:
print(f.read())
Answer: B
The write() method does not add a newline automatically. Both write() calls append their strings directly, so the file contains 'Hello World'. When read back, it prints 'Hello World'.
Q.3Medium
Which method reads all lines of a file and returns them as a list of strings, with each string ending in a newline character?
Answer: C
readlines() reads the entire file and returns a list where each element is one line including the newline character '\n'. read() returns the entire content as a single string, readline() reads only one line at a time, and readall() is not a standard Python file method.
Q.4Medium
What does the seek(0) method do when called on an open file object?
Answer: B
seek(n) moves the file pointer to byte position n. seek(0) moves the pointer to the very beginning (byte 0) of the file, allowing subsequent reads to start from the beginning. tell() is used to return the current position, not seek().
Q.5Medium
What will be the output of the following code?
f = open('data.txt', 'w')
f.write('Line 1\nLine 2\nLine 3')
f.close()
f = open('data.txt', 'r')
print(len(f.readlines()))
f.close()
Answer: C
The string written contains two '\n' characters, splitting the content into three lines: 'Line 1\n', 'Line 2\n', and 'Line 3'. readlines() returns a list of these three lines, so len() returns 3.
Advertisement
Q.6Medium
Which of the following is the correct way to open a file in binary write mode in Python?
Answer: B
Binary mode is specified by appending 'b' after the primary mode character. 'wb' means write in binary mode. 'b' alone is not a valid mode, 'bw' is an invalid ordering, and 'binary' is not a recognized mode string in Python.
Q.7Medium
What is the advantage of using the 'with' statement when working with files in Python?
Answer: B
The 'with' statement uses a context manager that guarantees the file's __exit__ method is called, which closes the file automatically when the block ends — even if an exception is raised. This prevents resource leaks without requiring explicit f.close() calls.
Q.8Medium
What will the following code print?
with open('sample.txt', 'w') as f:
f.write('Python\nFile Handling')
with open('sample.txt', 'r') as f:
line = f.readline()
print(line.strip())
Answer: C
readline() reads only the first line up to and including the newline character, returning 'Python\n'. strip() removes the trailing newline, so the output is 'Python'. The second line 'File Handling' is never read.
Q.9Medium
What exception is raised when you try to open a file that does not exist using open('missing.txt', 'r')?
Answer: C
In Python 3, attempting to open a non-existent file in read mode raises FileNotFoundError, which is a subclass of OSError. While catching OSError would also work, the specific exception raised is FileNotFoundError. FileExistsError is raised when trying to create a file that already exists (mode 'x').
Q.10Medium
What will be the output of the following code?
with open('test.txt', 'w') as f:
f.write('Hello World')
with open('test.txt', 'a') as f:
f.write('!!!')
with open('test.txt', 'r') as f:
print(f.read())
Answer: C
The first block writes 'Hello World' to the file. The second block opens the file in append mode ('a'), which does not truncate the file but moves the pointer to the end, then appends '!!!'. Reading the file finally gives 'Hello World!!!'.