Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / File Handling
mcq
Direction: Choose the correct option

Q1.

Which function is used to open a file in Python?
A. io.open()
B. file()
C. open()
D. Both A and C
Direction: Choose the correct option

Q2.

What does `open('file.txt', 'r')` do?
A. Opens file.txt for appending
B. Creates a new file
C. Opens file.txt for writing
D. Opens file.txt for reading
Direction: Choose the correct option

Q3.

How do you read all lines of a file into a list?
A. f.read()
B. f.readline()
C. f.readall()
D. f.readlines()
Direction: Choose the correct option

Q4.

What is the correct way to write to a file?
A. f.add('text')
B. f.print('text')
C. f.writelines('text')
D. f.write('text')
Direction: Choose the correct option

Q5.

What does the `with` statement do in file handling?
A. Locks the file
B. Creates a backup
C. Opens the file
D. Automatically closes the file after the block
Direction: Choose the correct option

Q6.

What mode would you use to open a file for writing, creating if not exists?
A. 'x'
B. 'a'
C. 'r'
D. 'w'
Direction: Choose the correct option

Q7.

How do you check if a file exists before opening?
A. os.isfile()
B. path.exists()
C. os.path.exists()
D. file.exists()
Direction: Choose the correct option

Q8.

What is the difference between 'w' and 'a' modes?
A. 'w' appends, 'a' overwrites
B. Both append
C. Both overwrite
D. 'w' overwrites, 'a' appends
Direction: Choose the correct option

Q9.

What does `f.seek(0)` do?
A. Sets the file position to 0
B. Both A and C
C. Moves to end
D. Moves the file pointer to the beginning
Direction: Choose the correct option

Q10.

What is the output of `open('test.txt', 'x')` if file already exists?
A. Creates a new file
B. Overwrites
C. FileExistsError
D. None