Introduction to Programming Using Python, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 13 Files and Exceptions Handling


Section 13.2 Text Input and Output
13.1  To open a file c:\scores.txt for reading, use __________.
A. infile = open("c:\scores.txt", "r")
B. infile = open("c:\\scores.txt", "r")
C. infile = open(file = "c:\scores.txt", "r")
D. infile = open(file = "c:\\scores.txt", "r")

13.2  To open a file c:\scores.txt for writing, use __________.
A. outfile = open("c:\scores.txt", "w")
B. outfile = open("c:\\scores.txt", "w")
C. outfile = open(file = "c:\scores.txt", "w")
D. outfile = open(file = "c:\\scores.txt", "w")

13.3  To open a file c:\scores.txt for appending data, 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")

13.4  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, an error occurs.
C. When you open a file for reading, if the file does not exist, the program will open an empty file.
D. When you open a file for writing, if the file does not exist, a new file is created.
E. When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

13.5  To read two characters from a file object infile, use _________.
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()

13.6  To read the entire remaining contents of the file as a string from a file object infile, use _________.
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()

13.7  To read the next line of the file from a file object infile, use _________.
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()

13.8  To read the remaining lines of the file from a file object infile, use _________.
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()

13.9  The readlines() method returns a ____________.
A. str
B. a list of lines
C. a list of single characters
D. a list of integers

13.10  The ______ function can be used to check if a file f exists.
A. os.path.isFile(f)
B. os.path.exists(f)
C. os.path.isfile(f)
D. os.isFile(f)

Section 13.3 File Dialogs
13.11  _____________ displays a file dialog for opening an existing file.
A. filename = askopenfilename()
B. filename = asksaveasfilename()
C. filename = openfilename()
D. filename = saveasfilename()

13.12  _____________ displays a file dialog for saving a file.
A. filename = askopenfilename()
B. filename = asksaveasfilename()
C. filename = openfilename()
D. filename = saveasfilename()

Section 13.5 Retrieving Data from the Web
13.13  _____________ opens a URL for input.
A. infile = urllib.request.urlopen(urlString)
B. infile = urllib.urlopen(urlString)
C. infile = request.urlopen(urlString)
D. infile = urlopen(urlString)

13.14  Invoking the ___________ method converts raw byte data to a string.
A. encode()
B. decode()
C. convert()
D. toString()

Section 13.6 Exception Handling
13.15  What is displayed when the following program is run?

try:
    list = 5 * [0]
    x = list[5]
    print("Done")
except IndexError: 
    print("Index out of bound")
A. "Done" followed by "Index out of bound"
B. "Index out of bound"
C. "Done"
D. Nothing displayed

13.16  What is displayed when the following program is run?

def main():
    try:
        f()
        print("After the function call")
    except ZeroDivisionError:
        print("Divided by zero!")
    except:
        print("Exception")

def f(): 
    print(1 / 0)

main()
A. "After the function call" followed by "Divided by zero!"
B. "After the function call"
C. "Divided by zero!"
D. "Divided by zero!" followed by "Exception"

13.17  What is displayed when the following program is run?

try:
    list = 10 * [0]
    x = list[9]
    print("Done")
except IndexError: 
    print("Index out of bound")
else: 
    print("Nothing is wrong")
finally: 
    print("Finally we are here")
A. "Done" followed by "Nothing is wrong"
B. "Done" followed by "Nothing is wrong" followed by "Finally we are here"
C. "Index out of bound" followed by "Nothing is wrong" followed by "Finally we are here"
D. "Nothing is wrong" followed by "Finally we are here"

13.18  What is displayed when the following program is run?

try:
    list = 10 * [0]
    x = list[10]
    print("Done")
except IndexError: 
    print("Index out of bound")
else: 
    print("Nothing is wrong")
finally: 
    print("Finally we are here")
A. "Done" followed by "Nothing is wrong"
B. "Done" followed by "Nothing is wrong" followed by "Finally we are here"
C. "Index out of bound" followed by "Nothing is wrong" followed by "Finally we are here"
D. "Nothing is wrong" followed by "Finally we are here"
E. "Index out of bound" followed by "Finally we are here"

Section 13.10 Binary IO Using Picking
13.19  To open a file c:\scores.dat for binary writing, use __________.
A. outfile = open("c:\\scores.dat", "wb")
B. outfile = open("c:\\scores.dat", "w")
C. outfile = open("c:\scores.dat", "a")
D. outfile = open("c:\\scores.dat", "w")

13.20  To open a file c:\scores.dat for binary reading, use __________.
A. infile = open("c:\\scores.dat", "rb")
B. infile = open("c:\\scores.dat", "r")
C. infile = open("c:\scores.dat", "wrb")
D. infile = open("c:\\scores.dat", "r")

13.21  Whihc function do you use to write data to perform binary output?
A. write
B. output
C. dump
D. send

13.22  Whihc function do you use to read data using binary input?
A. read
B. input
C. load
D. receive