13. Files and Exceptions Handling - 22 Questions


13. Files and Exceptions Handling

13. Files and Exceptions Handling

22 Questions
Created by Y. Daniel Liang - http://www.cs.armstrong.edu/liang/index.html
Free
An exception is an error that happens during the execution of a program. Exceptions are known to non-programmers as instances that do not conform to a general rule. The name "exception" in computer science has this meaning as well: It implies that the problem (the exception) doesn't occur frequently, i.e. the exception is the "exception to the rule". Exception handling is a construct in Python to handle or deal with errors automatically.
  1. To open a file c:\scores.txt for reading, use __________.



    infile = open("c:\scores.txt", "r")
    infile = open("c:\\scores.txt", "r")
    infile = open(file = "c:\scores.txt", "r")
    infile = open(file = "c:\\scores.txt", "r")

    View Answer | Discuss in forum
  2. To open a file c:\scores.txt for writing, use __________.



    outfile = open("c:\scores.txt", "w")
    outfile = open("c:\\scores.txt", "w")
    outfile = open(file = "c:\scores.txt", "w")
    outfile = open(file = "c:\\scores.txt", "w")

    View Answer | Discuss in forum
  3. To open a file c:\scores.txt for appending data, use ________



    outfile = open("c:\\scores.txt", "a")
    outfile = open("c:\\scores.txt", "rw")
    outfile = open(file = "c:\scores.txt", "w")
    outfile = open(file = "c:\\scores.txt", "w")

    View Answer | Discuss in forum
  4. Which of the following statements is true?



    When you open a file for reading, if the file does not exist, an error occurs.
    When you open a file for writing, if the file does not exist, an error occurs.
    When you open a file for reading, if the file does not exist, the program will open an empty file.
    None of these

    View Answer | Discuss in forum
  5. To read two characters from a file object infile, use _________.



    infile.read(2)
    infile.read()
    infile.readline()
    infile.readlines()

    View Answer | Discuss in forum
  6. To read the entire remaining contents of the file as a string from a file object infile, use _________.



    infile.read(2)
    infile.read()
    infile.readline()
    infile.readlines()

    View Answer | Discuss in forum
  7. To read the next line of the file from a file object infile, use _________.



    infile.read(2)
    infile.read()
    infile.readline()
    infile.readlines()

    View Answer | Discuss in forum
  8. To read the remaining lines of the file from a file object infile, use _________.



    infile.read(2)
    infile.read()
    infile.readline()
    infile.readlines()

    View Answer | Discuss in forum
  9. The readlines() method returns a ____________.



    str
    a list of lines
    a list of single characters
    a list of integers

    View Answer | Discuss in forum
  10. The ______ function can be used to check if a file f exists.



    os.path.isFile(f)
    os.path.exists(f)
    os.path.isfile(f)
    os.isFile(f)

    View Answer | Discuss in forum
  11. _____________ displays a file dialog for opening an existing file.



    filename = askopenfilename()
    filename = asksaveasfilename()
    filename = openfilename()
    filename = saveasfilename()

    View Answer | Discuss in forum
  12. _____________ displays a file dialog for saving a file.



    filename = askopenfilename()
    filename = asksaveasfilename()
    filename = openfilename()
    filename = saveasfilename()

    View Answer | Discuss in forum
  13. _____________ opens a URL for input.



    infile = urllib.request.urlopen(urlString)
    infile = urllib.urlopen(urlString)
    infile = request.urlopen(urlString)
    infile = urlopen(urlString)

    View Answer | Discuss in forum
  14. Invoking the ___________ method converts raw byte data to a string.



    encode()
    decode()
    convert()
    toString()

    View Answer | Discuss in forum
  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")



    "Done" followed by "Index out of bound"
    "Index out of bound"
    "Done"
    Nothing displayed

    View Answer | Discuss in forum
  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()



    "After the function call" followed by "Divided by zero!"
    "After the function call"
    "Divided by zero!"
    "Divided by zero!" followed by "Exception"

    View Answer | Discuss in forum
  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")



    "Done" followed by "Nothing is wrong"
    "Done" followed by "Nothing is wrong" followed by "Finally we are here"
    "Index out of bound" followed by "Nothing is wrong" followed by "Finally we are here"
    "Nothing is wrong" followed by "Finally we are here"

    View Answer | Discuss in forum
  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")



    "Done" followed by "Nothing is wrong"
    "Done" followed by "Nothing is wrong" followed by "Finally we are here"
    "Index out of bound" followed by "Nothing is wrong" followed by "Finally we are here"
    "Index out of bound" followed by "Finally we are here"

    View Answer | Discuss in forum
  19. To open a file c:\scores.dat for binary writing, use __________.



    outfile = open("c:\\scores.dat", "wb")
    outfile = open("c:\\scores.dat", "w")
    outfile = open("c:\scores.dat", "a")
    outfile = open("c:\\scores.dat", "w")

    View Answer | Discuss in forum
  20. To open a file c:\scores.dat for binary reading, use __________.



    infile = open("c:\\scores.dat", "rb")
    infile = open("c:\\scores.dat", "r")
    infile = open("c:\scores.dat", "wrb")
    infile = open("c:\\scores.dat", "r")

    View Answer | Discuss in forum
  21. Which function do you use to write data to perform binary output?



    write
    output
    dump
    send

    View Answer | Discuss in forum
  22. Which function do you use to read data using binary input?



    read
    input
    load
    receive

    View Answer | Discuss in forum