1.
Select the true statement:
Correct Answer
D. All of the mentioned
Explanation
When opening a file for reading, if the file does not exist, an error occurs. When opening a file for writing, if the file does not exist, a new file is created. And when opening a file for writing, if the file exists, the existing file is overwritten with the new file. Therefore, all of the mentioned statements are true.
2.
To read two characters from a file object infile, we use ____________
Correct Answer
A. Infile.read(2)
Explanation
To read two characters from a file object infile, we use "infile.read(2)". This method reads the specified number of characters from the file and returns them as a string. In this case, it will read two characters from the file and return them as a string.
3.
Which of the following mode will refer to binary data?
Correct Answer
D. B
Explanation
The mode "b" refers to binary data. In Python, when opening a file, the "b" mode can be used to read or write binary data. This mode is used when dealing with non-text files, such as images, audio files, or any other file that contains non-textual data. When reading or writing in binary mode, the data is treated as a sequence of bytes, rather than as text characters.
4.
What will be the output of the following code snippet?
colors = ['red\n', 'yellow\n', 'blue\n']
f = open('colors.txt', 'w')
f.writelines(colors)
f.close()
f.seek(0,0)
for line in f:
print (line)
Correct Answer
C. C. Error: I/O operation on closed file.
Explanation
The code snippet opens a file named "colors.txt" in write mode and writes the elements of the "colors" list to the file. Then, it closes the file. After that, it tries to read the contents of the file using a for loop, but since the file has already been closed, an error is raised indicating that there is an I/O operation on a closed file. Therefore, the output of the code will be an error message stating "Error: I/O operation on closed file."
5.
This statement correctly explains the function of seek() method.
Correct Answer
D. Move the current file position to a different location at a defined offset.
Explanation
The seek() method in file handling allows the user to move the current file position to a specified location at a defined offset. This means that the user can set the position within the file to a specific byte or character, allowing them to read or write data from that position onwards. This method is useful when the user wants to skip or jump to a specific part of the file without reading or writing all the data before it.
6.
Given the file dog_breeds.txt, which of the following is the correct way to open the file for reading as a text file? Select all that apply.
Correct Answer
C. Open('dog_breeds.txt', 'r')
Explanation
The correct way to open the file "dog_breeds.txt" for reading as a text file is by using the code: open('dog_breeds.txt', 'r'). This code specifies the file name and the mode 'r', which stands for "read". This allows the file to be opened in read-only mode, allowing the user to read the contents of the file without making any changes to it.
7.
What method is best if you have to read the entire file into a single string, when reading a file using the file object?
Correct Answer
B. .read()
Explanation
The best method to read the entire file into a single string when using the file object is .read(). This method reads the entire contents of the file and returns it as a single string. It is suitable when you want to read the entire file at once and store it in a string for further processing or manipulation. The other options listed (.readline(), .read_file_to_str(), .readlines()) do not provide the same functionality of reading the entire file into a single string.
8.
What does the <readlines()> method returns?
Correct Answer
B. A list of lines
Explanation
The method returns a list of lines. This means that when this method is called, it reads all the lines from the file or input and stores them as separate elements in a list. Each line is treated as a separate element in the list, allowing for easy access and manipulation of the lines of text.
9.
Which of the following functions do you use to write data in the binary format?
Correct Answer
C. Dump
Explanation
The correct answer is "dump." The dump function is commonly used to write data in binary format. It allows for the conversion of data from a structured format into a binary representation that can be stored or transmitted. This function is often used in programming languages to save data in a binary file or to send binary data over a network.
10.
Which of the following functions can be used to check if a file “logo” exists?
Correct Answer
C. Os.path.isfile(logo)
Explanation
The correct answer is os.path.isfile(logo). This function is used to check if a file exists at the specified path. It returns True if the path exists and it is a file, otherwise it returns False. In this case, it is used to check if a file named "logo" exists.