Python File Handling MCQ : Set 1

Python File Handling MCQ

1). What is file handling in Python?

a) A mechanism to handle errors in file operations
b) A way to create new files in Python
c) A process of working with files, such as reading from or writing to them
d) A method to encrypt files in Python

Correct answer is: c) A process of working with files, such as reading from or writing to them
Explanation: File handling in Python refers to the process of performing various operations on files, including reading data from files or writing data to files.

2). Which function is used to open a file in Python?

a) read()
b) open()
c) write()
d) close()

Correct answer is: b) open()
Explanation: The `open()` function in Python is used to open a file. It takes the filename as an argument and returns a file object that can be used to perform file operations.

3). How can you read the contents of a file in Python?

a) read(file)
b) open(file, ‘r’)
c) write(file)
d) close(file)

Correct answer is: b) open(file, ‘r’)
Explanation: To read the contents of a file in Python, you need to use the `open()` function with the file name as the first argument and `’r’` as the second argument. This opens file in the read mode.

4). Which of the following modes is used to open a file for the writing in Python?

a) ‘r’
b) ‘w’
c) ‘a’
d) ‘x’

Correct answer is: b) ‘w’
Explanation: The mode `’w’` in Python is used to open a file for the writing. If file already exists, it will be truncated. If file does not exists, a new file will be created.

5). By which method can you write data to a file in Python?

a) open(file, ‘w’)
b) write(file)
c) read(file)
d) close(file)

Correct answer is: a) open(file, ‘w’)
Explanation: To write data to a file in Python, you need to open the file in write mode using the `open()` function with the file name as the first argument and `’w’` as the second argument. This opens the file for writing.

6). What is the purpose of the `close()` function in file handling?

a) To delete a file
b) To rename a file
c) To close the file after performing file operations
d) To check if a file exists

Correct answer is: c) To close the file after performing file operations
Explanation: The `close()` function is used to close the file after performing file operations. It releases the resources associated with the file and ensures that any changes made to the file are saved.

7). Which of the following methods is used to read a single line from a file in Python?

a) read()
b) readlines()
c) readline()
d) readall()

Correct answer is: c) readline()
Explanation: The `readline()` method is used to read a single line from a file in Python. It reads characters from the current position in the file until it encounters a newline character.

8). What does the `with` statement do in file handling?

a) Opens a file for reading
b) Closes a file after performing file operations
c) Deletes a file from the system
d) Renames a file

Correct answer is: b) Closes a file after performing file operations
Explanation: The `with` statement is used in file handling to ensure that the file is properly closed after performing file operations. It automatically closes the file.

9). Which of the following modes is used to open a file for both reading and writing in Python?

a) ‘r’
b) ‘w’
c) ‘a’
d) ‘r+’

Correct answer is: d) ‘r+’
Explanation: The mode `’r+’` is used to open a file for both reading and writing in Python. It allows you to read from and write to the file at any position.

10). Which method is used to write data to a file in Python?

a) read()
b) write()
c) append()
d) open()

Correct answer is: b) write()
Explanation: The `write()` method is used to write data to a file in Python. It takes a string as an argument and writes it in the file.

11). Which mode is used to open a file for appending data in Python?

a) ‘a’
b) ‘w’
c) ‘r’
d) ‘x’

Correct answer is: a) ‘a’
Explanation: The mode `’a’` in Python is used to open a file for appending data. If file does not exist, it will be created. If file exists, data will be appended to the end of the file.

12). How can you read the contents of a file as a list of lines in Python?

a) read()
b) readlines()
c) readline()
d) openlines()

Correct answer is: b) readlines()
Explanation: The `readlines()` method is used to read the contents of a file as a list of lines in Python. Each line is stored as an element in the list.

13). Which method is used to move the file pointer to a specific position in a file?

a) seek()
b) move()
c) position()
d) setpos()

Correct answer is: a) seek()
Explanation: The `seek()` method is used to move the file pointer to a specific position in a file. It takes two arguments: the offset (number of bytes to move) and the optional `from_` argument, which specifies the reference position.

14). What is the purpose of the `tell()` method in file handling?

a) To check if a file exists
b) To rename a file
c) To return the current position of the file pointer
d) To check the size of a file

Correct answer is: c) To return the current position of the file pointer
Explanation: The `tell()` method is used to return the current position of the file pointer in a file. It returns an integer representing the offset in bytes from the beginning of the file.

15). Which method is used to check if the end of a file has been reached?

a) seek()
b) endoffile()
c) eof()
d) atend()

Correct answer is: c) eof()
Explanation: There is no built-in method to directly check if the end of a file has been reached. However, the `eof()` function is commonly used in file handling to determine if the end of the file has been reached based on the return value of other file reading methods.

16). How can you delete a file using Python?

a) delete(file)
b) remove(file)
c) delete(file)
d) erase(file)

Correct answer is: b) remove(file)
Explanation: The `remove()` function from the `os` module is used to delete a file in Python. It takes the filename as an argument and deletes the file from the file system.

17). Which method is used to check if a file is closed in Python?

a) is_closed()
b) isclosed()
c) closed()
d) close()

Correct answer is: b) isclosed()
Explanation: The `isclosed()` method is used to check if a file is closed in Python. It returns `True` if the file is closed and `False` otherwise.

18). What is the purpose of the `flush()` method in file handling?

a) To close the file after performing file operations
b) To clear the contents of a file
c) To ensure that any pending writes are immediately written to the file
d) To check if a file exists

Correct answer is: c) To ensure that any pending writes are immediately written to the file
Explanation: The `flush()` method is used to ensure that any pending writes are immediately written to the file. It is useful in situations where you want to make sure that all data is written to the file before proceeding.

19). Which of the following is NOT a valid file mode in Python?

a) ‘x’
b) ‘t’
c) ‘b’
d) ‘a+’

Correct answer is: b) ‘t’
Explanation: The mode `’t’` is not a valid file mode in Python. The available modes are `’r’` (read), `’w’` (write), `’a’` (append), and their combinations with `’+’` (read and write).

20). What happens if you try to open a non-existing file in read mode in Python?

a) A new file with the same name is created
b) An error is raised
c) The program waits until the file is created
d) The program continues without any issues

Correct answer is: b) An error is raised
Explanation: If you try to open a non-existing file in read mode using the `open()` function, Python will raise a `FileNotFoundError` indicating that the file does not exist.

Leave a Comment