Python File Handling MCQ : Set 2

Python File Handling MCQ

1). Which method is used to truncate a file to a specified size in Python?

a) truncate()
b) resize()
c) reduce()
d) shrink()

Correct answer is: a) truncate()
Explanation: The `truncate()` method is used to truncate a file to a specified size in Python. If the specified size is smaller than the current size, the file will be truncated. If it is larger, the file size will be increased, and null bytes will be added.

2). Which of the following methods is used to read a specific number of bytes from a file in Python?

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

Correct answer is: d) read(size)
Explanation: The `read(size)` method is used to read a specific number of bytes from a file in Python. It takes an integer `size` as an argument and returns the specified number of bytes from the file.

3). How can you create a new file and write data to it in Python?

a) create(file, ‘w’)
b) write(file, ‘w’)
c) open(file, ‘w’)
d) make(file, ‘w’)

Correct answer is: c) open(file, ‘w’)
Explanation: To create a new file and write data to it in Python, you need to use the `open()` function with the file name as the first argument and `’w’` as the second argument. This opens the file in write mode and creates a new file if it does not exist.

4). Which method is used to iterate over the lines of a file object in Python?

a) iterate()
b) lines()
c) readlines()
d) readline()

Correct answer is: c) readlines()
Explanation: The `readlines()` method is used to iterate over the lines of a file object in Python. It returns a list of lines, which can be iterated over using a loop or other iteration methods.

5). What happens if you try to open a file in write mode that already exists?

a) The existing file will be overwritten with the new content
b) An error is raised
c) The existing file will be truncated
d) The program waits until the file is deleted

Correct answer is: a) The existing file will be overwritten with the new content
Explanation: If you open a file in write mode using the `open()` function and the file already exists, the existing file will be overwritten with the new content. Any previous content in the file will be lost.

6). Which method is used to copy a file in Python?

a) copyfile()
b) move()
c) duplicate()
d) os.copy()

Correct answer is: a) copyfile()
Explanation: The `copyfile()` function from the `shutil` module is used to copy a file in Python. It takes the source file path and the destination file path as arguments and copies the contents of the source file to the destination file.

7). How can you read the contents of a file as a single string in Python?

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

Correct answer is: a) read()
Explanation: The `read()` method is used to read the contents of a file as a single string in Python. It reads the entire content of the file and returns it as a string.

8). Which of the following is NOT a valid method to close a file in Python?

a) close()
b) flush()
c) exit()
d) using the `with` statement

Correct answer is: c) exit()
Explanation: The `exit()` method is not used to close a file in Python. It is a built-in method used to exit the Python interpreter. The correct method to close a file is by calling the `close()` method or using the `with` statement.

9). Which method is used to read a file line by line and process each line individually in Python?

a) read()
b) readlines()
c) readline()
d) iterating over the file object

Correct answer is: d) iterating over the file object
Explanation: You can read a file line by line and process each line individually in Python by iterating over the file object. When you iterate over a file object, each iteration returns a line from the file.

10). Which method is used to read a specific number of characters from a file in Python?

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

Correct answer is: a) read()
Explanation: The `read()` method is used to read a specific number of characters from a file in Python. If no size is specified, it reads the entire content of the file.

11). Which method is used to write multiple lines of data to a file in Python?

a) writeline()
b) writelines()
c) write()
d) writelines()

Correct answer is: d) writelines()
Explanation: The `writelines()` method is used to write multiple lines of data to a file in Python. It takes a list of strings as an argument and writes each string as a separate line in the file.

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

a) write()
b) writebytes()
c) writebin()
d) writebinary()

Correct answer is: b) writebytes()
Explanation: The `writebytes()` method is used to write binary data to a file in Python. It takes a bytes-like object as an argument and writes the binary data to the file.

13). How can you read a file in chunks in Python?

a) read(size)
b) readlines()
c) readline()
d) using a loop and read(size) method

Correct answer is: d) using a loop and read(size) method
Explanation: You can read a file in chunks in Python by using a loop and the `read(size)` method. In each iteration, you can read a specified number of bytes from the file.

14). Which method is used to move a file from one location to another in Python?

a) move()
b) rename()
c) copyfile()
d) os.move()

Correct answer is: a) move()
Explanation: The `move()` method from the `shutil` module is used to move a file from one location to another in Python. It takes the source file path and the destination file path as arguments and moves the file to the specified destination.

15). What is the mode used to open a file for both reading and writing?

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

Correct answer is: d
Explanation: The mode r+ is used to open a file for both reading and writing. It will allow you to read and write to the file.

16). What is the difference between the modes r and r+?

a) The mode r can only read the contents of the file, while the mode r+ can read and write to the file.
b) The mode r will only read the contents of the file if it already exists, while the mode r+ will create a new file if it does not exist.
c) The mode r will not allow you to change the contents of the file, while the mode r+ will allow you to change the contents of the file.
d) Both modes are the same.

Correct answer is: a
Explanation: The mode r can only read the contents of the file, while the mode r+ can read and write to the file.

17). What is the difference between the modes w and w+?

a) The mode w will overwrite the contents of the file if it already exists, while the mode w+ will create a new file if it does not exist.
b) The mode w will not allow you to change the contents of the file, while the mode w+ will allow you to change the contents of the file.
c) Both modes are the same.
d) None of the above.

Correct answer is: a
Explanation: The mode w will overwrite the contents of the file if it already exists, while the mode w+ will create a new file if it does not exist.

18). What is the difference between the modes a and a+?

a) The mode a will append the contents of the file to the end of the file, while the mode a+ will allow you to read and append to the file.
b) The mode a will not allow you to change the contents of the file, while the mode a+ will allow you to change the contents of the file.
c) Both modes are the same.
d) None of the above.

Correct answer is: a
Explanation: The mode a will append the contents of the file to the end of the file, while the mode a+ will allow you to read and append to the file.

19). What is the output of the following code?

				
					file = open("file1", "r")
print(file.read())
file.close()
				
			

a) It will print the content of the file named “file1” to the console.
b) It will raise a FileNotFoundError because the file named “file1” does not exist.
c) It will raise a PermissionError because the file “file1” is opened in read-only mode.
d) It will print an empty string to the console because the file named “file1” is empty.

Correct answer is: a) It will print the content of the file named “file1” to the console.
Explanation: The code opens a file named “file1” in read mode using the `open()` function. Then, the `read()` method is called on the file object to read the entire content of the file. Finally, the content is printed to the console using the `print()` function.

20). What is the output of the following code?

				
					file = open("file1", "w")
file.write("This line writes into file")
file.close()
				
			

a) The code will raise a `FileNotFoundError` because the file “file1” does not exist.
b) The code will create a new file named “file1” and write the text “This line writes into file” into it.
c) The code will open an existing file named “file1” and append the text “This line writes into file” to its contents.
d) The code will raise a `PermissionError` because the file “file1” is read-only.

Correct answer is: b) The code will create a new file named “file1” and write the text “This line writes into file” into it.
Explanation: The code opens a file named “file1” in write mode using the `”w”` parameter. If the file already exists, its contents will be truncated. Since the file does not exist initially, a new file named “file1” will be created. The code then writes the string “This line writes into file” to the file using the `write()` method. Finally, the file is closed.

Leave a Comment