Skip to main content

Exercises 8.11 Multiple Choice Questions

1.

    Q-1: Which of the following can be used to open a file called myText.txt in read-only mode?
  • infile = open("myText.txt", “w”)
  • This will open in write-only mode.
  • infile = open("myText.txt", r)
  • The r must be in quotes.
  • infile = open("myText.txt", “read”)
  • Use "r" for read-only mode.
  • infile = open("myText.txt")
  • The default is read if no mode is specified

2.

    Q-2: Which of the following can be used to open a file called myText.txt in write-only mode?
  • outfile = open("myText.txt", w)
  • The w must be in quotes.
  • outfile = open("myText.txt", “write”)
  • Use "w" for write-only mode.
  • outfile = open("myText.txt", “w”)
  • This will open the file in write-only mode.
  • outfile = open("myText.txt")
  • This would open the file in read-only mode.

3.

    Q-3: Which command below closes the already open file myText.txt if the following code has already been written?
    ref_file = open("myText.txt", "r")
    
  • close()
  • You must call this method on the file object.
  • ref_file.close()
  • This closes the file using the variable it has been assigned to.
  • close(ref_file)
  • The command close() needs to be called on the file object using dot notation.
  • close("myText")
  • The command close() needs to be called on the file object using dot notation.

4.

    Q-4: Which of the commands below is used to add the following string to the end of a file object filevar?
    somestring = "my Sentence"
    
  • filevar.append(somestring)
  • Append is a command used for lists, not files.
  • filevar.write("somestring")
  • somestring is a variable and does not need quotation marks around it.
  • filevar.write(somestring)
  • Using dot notation, we can call the write command with the string variable inside the parentheses.
  • somestring.write()
  • The command write() needs to be called on the file object, not the string itself.

5.

    Q-5: The contents of names.txt is listed here:
    Moana
    Cinderella
    Tiana
    
    Which of the following code blocks will print all of the names in names.txt?
    I
    names = open("names.txt", "r")
    for line in names:
        print(names)
    
    II
    names = open("names.txt", "r")
    for line in names:
        print(line)
    
    III
    names = open("names.txt", "r")
    for line in names:
        print("line")
    
  • I
  • Append is a command used for lists, not files.
  • II
  • somestring is a variable and does not need quotation marks around it.
  • III
  • Using dot notation, we can call the write command with the string variable inside the parentheses.
  • None of the above.

6.

    Q-6: What keyword do you use to specify code to execute if there is an error when the body of the try executes?
  • catch
  • This is used in other languages, but not in Python
  • except
  • Use except to execute code when an error occurs.
  • exception
  • Close, but it is shorter.
  • error
  • Try again.

7.

    Q-7: How many errors are in the code below? It should open the file in read-only mode, read each line and print each line and then close the file.
    def print_contents(file)
        file_obj = open(file)
        for line in "file_obj":
            print(line_obj)
    
  • 1
  • Try again
  • 2
  • Try again
  • 3
  • Try again
  • 4
  • The first line is missing a ":", the third shouldn't have file_obj in quotes, the fourth should use line rather than line_obj, and the close is missing.