Which of the following statements is true regarding the opening modes of a file?

Q-3: Which command below closes the already open file myText.txt if the following code has already been written?

Show

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.
    • Q-4: Which of the commands below is used to add the following string to the end of a file object filevar?

    • 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.

      Q-5: The contents of names.txt is listed here:

      Which of the following code blocks will print all of the names in names.txt?

      names = open("names.txt", "r")
      for line in names:
          print(names)
      
      1

      names = open("names.txt", "r")
      for line in names:
          print(names)
      

      names = open("names.txt", "r")
      for line in names:
          print(names)
      
      2

      names = open("names.txt", "r")
      for line in names:
          print(line)
      

      names = open("names.txt", "r")
      for line in names:
          print(names)
      
      3

      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.

      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.

      This Python file handling quiz provides multiple-choice questions(MCQ) to familiarize with Python file operations. This quiz test your knowledge of file operations such as opening a file, reading a file, writing a file, closing it, renaming a file, deleting a file, and various file methods.

      Read Python File Handling to solve this quiz.

      • This quiz contains 12 Questions. Solve 8 correct to pass the test.
      • You will have to read all the given answers and click over the correct answer.

      1. Select the correct mode to open a file for appending as well as reading

       a+

       ar

       rw

       ar+

      2. Select the incorrect file access mode

       r

       ab+

       rw+

       wb+

      3. Select the correct access mode to open a file only for exclusive creation

       t

       w

       ar0

       ar1

      4. If the file is opened in write mode and already exists, it truncates the existing content and places the filehandle at the beginning of the file.

       True

       False

      5. Select the correct method to write a list of lines to a file

       ar2

       ar3

       ar4

      6. Select all true statements when a file is opened using the ar5 statement

       The with statement simplifies exception handling

       The file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.

      Welcome to the Python file handling quiz part-1. In this quiz, we’ve added 15 basic questions that cover topics related to file handling functions and access modes.

      Undoubtedly, the files are a key component of almost all applications. But it has been a challenge for programmers to use them in an optimized manner. Because the mishandling of files can pull down the performance of any application miserably.

      This topic is important for programmers but has the same relevance for automation testers. One could use it for file processing software like Accounting or Hospitality management whereas another may find it useful for reading configuration files or logging errors.

      If you are a software engineer or an automation tester just beginning to learn Python and file handling, then better walk through the following tutorial. It’ll give you a detailed overview of the Python file handling and demonstrate their usage with examples.

      ➡ Python File Handling Tutorial

      Now, attempt the below Python file handling quiz part-1 and answer each question carefully.

      Python File Handling Quiz Part-1 for Beginners

      Q-1.  Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

      A. infile = open(“c:\temp.txt”, “r”)
      B. infile = open(“c:\\temp.txt”, “r”)
      C. infile = open(file = “c:\temp.txt”, “r+”)
      D. infile = open(file = “c:\\temp.txt”, “r+”)

      Click here to view the answer.

      Answer. B

       

      Q-2.  Which of the following command is used to open a file “c:\temp.txt” in write-mode only?

      A. outfile = open(“c:\temp.txt”, “w”)
      B. outfile = open(“c:\\temp.txt”, “w”)
      C. outfile = open(file = “c:\temp.txt”, “w+”)
      D. outfile = open(file = “c:\\temp.txt”, “w+”)

      Click here to view the answer.

      Answer. B

       

      Q-3.  Which of the following command is used to open a file “c:\temp.txt” in append-mode?

      A. outfile = open(“c:/temp.txt”, “a”)
      B. outfile = open(“c:\\temp.txt”, “rw”)
      C. outfile = open(“c:\temp.txt”, “w+”)
      D. outfile = open(“c:\\temp.txt”, “r+”)
      E. outfile = open(“c:\\temp.txt”, “a”)

      Click here to view the answer.

      Answer. A and E

       

      Q-4.  Which of the following statements are true regarding the opening modes of a file?

      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.

      Click here to view the answer.

      Answer. A, D and E

       

      Q-5. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

      A. file.read(n)
      B. n = file.read()
      C. file.readline(n)
      D. file.readlines()

      Click here to view the answer.

      Answer. A

      Q-6. Which of the following commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?

      A. tmpfile.read(n)
      B. tmpfile.read()
      C. tmpfile.readline()
      D. tmpfile.readlines()

      Click here to view the answer.

      Answer. B

       

      Q-7. Which of the following commands can be used to read the next line in a file using the file object <tmpfile>?

      A. tmpfile.read(n)
      B. tmpfile.read()
      C. tmpfile.readline()
      D. tmpfile.readlines()

      Click here to view the answer.

      Answer. C

       

      Q-8. Which of the following commands can be used to read the remaining lines in a file using the file object <tmpfile>?

      A. tmpfile.read(n)
      B. tmpfile.read()
      C. tmpfile.readline()
      D. tmpfile.readlines()

      Click here to view the answer.

      Answer. D

       

      Q-9. What does the <readlines()> method returns?

      A. str
      B. a list of lines
      C. list of single characters
      D. list of integers

      Click here to view the answer.

      Answer. B

       

      Q-10. Which of the following functions can be used to check if a file “logo” exists?

      A. os.path.isFile(logo)
      B. os.path.exists(logo)
      C. os.path.isfile(logo)
      D. os.isFile(logo)

      Click here to view the answer.

      Answer. C

       

      Q-11. Which of the following functions displays a file dialog for opening an existing file?

      A. tmpfile = askopenfilename()
      B. tmpfile = asksaveasfilename()
      C. tmpfile = openfilename()
      D. tmpfile = saveasfilename()

      Click here to view the answer.

      Answer. A

       

      Q-12. Which of the following functions displays a file dialog for saving a file?

      A. tmpfile = askopenfilename()
      B. tmpfile = openfilename()
      C. tmpfile = asksaveasfilename()
      D. tmpfile = saveasfilename()

      Click here to view the answer.

      Answer. C

       

      Q-13. Which of the following command is used to open a file “c:\temp.txt” for writing in binary format only?

      A. outfile = open(“c:\temp.txt”, “w”)
      B. outfile = open(“c:\\temp.txt”, “wb”)
      C. outfile = open(“c:\temp.txt”, “w+”)
      D. outfile = open(“c:\\temp.txt”, “wb+”)

      Click here to view the answer.

      Answer. B

       

      Q-14. Which of the following command is used to open a file “c:\temp.txt” for reading in binary format only?

      A. outfile = open(“c:\temp.txt”, “r”)
      B. outfile = open(“c:\\temp.txt”, “rb”)
      C. outfile = open(“c:\temp.txt”, “r+”)
      D. outfile = open(“c:\\temp.txt”, “rb+”)

      Click here to view the answer.

      Answer. B

       

      Q-15. Which of the following functions do you use to write data in the binary format?

      A. write
      B. output
      C. dump
      D. send

      Click here to view the answer.

      Answer. C

       

      Summary – Python File Handling Quiz Part-1 for Beginners

      We hope that you’d enjoyed going through the above Python file handling quiz part-1. Though, you could have felt the questions a bit basic but would have reminded you of the core file handling principals in Python.

      Soon, we’ll also come up with the part-2 of the file handling series. Till then, continue reading and keep learning.

      Which of the following statements are true when you open a file for writing?

      When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

      Which of the following are file opening modes in Python?

      Opening Files in Python.

      Which of the following method can be used to open a file in file handling?

      Opening a file The fopen() function is used to create a file or open an existing file: fp = fopen(const char filename,const char mode); There are many modes for opening a file: r - open a file in read mode.

      Which of the following is not a method of opening files?

      Explanation: ios::trunc is used to truncate a file if it exists. It is not a file opening mode.