Test Series - python

Test Number 97/108

Q: To open a file c:scores.txt for appending data, we use ____________
A. outfile = open(“c:\scores.txt”, “a”)
B. outfile = open(“c:\scores.txt”, “rw”)
C. outfile = open(file = “c:scores.txt”, “w”)
D. outfile = open(file = “c:\scores.txt”, “w”)
Solution: a is used to indicate that data is to be appended.
Q: Which of the following statements are true?
A. When you open a file for reading, if the file does not exist, an error occurs
B. When you open a file for writing, if the file does not exist, a new file is created
C. When you open a file for writing, if the file exists, the existing file is overwritten with the new file
D. All of the mentioned
Solution: The program will throw an error.
Q: To read two characters from a file object infile, we use ____________
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Solution: Execute in the shell to verify.
Q: To read the entire remaining contents of the file as a string from a file object infile, we use ____________
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Solution: read function is used to read all the lines in a file.
Q: What will be the output of the following Python code?

f = None
for i in range (5):
    with open("data.txt", "w") as f:
        if i > 2:
            break
print(f.closed)
A. True
B. False
C. None
D. Error
Solution: The WITH statement when used with open file guarantees that the file object is closed when the with block exits.
Q: To read the next line of the file from a file object infile, we use ____________
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Solution: Execute in the shell to verify.
Q: To read the remaining lines of the file from a file object infile, we use ____________
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Solution: Execute in the shell to verify.
Q: The readlines() method returns ____________
A. str
B. a list of lines
C. a list of single characters
D. a list of integers
Solution: Every line is stored in a list and returned.

You Have Score    /8