Python Programming Quiz

Quiz on File Handling in Python

Time left: 300 seconds

Q.1. Which of the following true about FILE *fp

Select Your Option

Option B is the standard way to open a file using open() function. Execute help(open) to get more details.

Q.2. Select the correct mode to open a file for appending as well as reading:

Select Your Option

a+ mode opens a file for both the reading and appending. The pointer will be placed at the end of the file and new content will be written after the existing content.

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

Select Your Option

Option C = x: This opens a file only for exclusive creation. If the file already exists, this operation fails.

Q.4. Which of the following statements are true?

Select Your Option

All of the given statements are true regarding files in Python.

Q.5. What will be the output of the following Python statement?
          
            f = None
            for i in range (5):
                with open("data.txt", "w") as f:
                    if i > 2:
                        break
            print(f.closed)
          
          

Select Your Option

The WITH statement when used with open file guarantees that the file object is closed when the with block exits.

Q.6. When reading a file using the file object, what method is best for reading the entire file into a single string?

Select Your Option

The .read_file_to_str() might seem like the right one, it is in fact not a method of the builtin file object. The easiest option is to use the .read() method. Remember though that if you pass in a integer value, the entire file is no longer read and instead n number of bytes will be read and returned back, where n is the integer value you passed in.

Q.7. Which method is used to read file line by line

Select Your Option

read(): Returns the entire file content and it accepts the optional size parameter that mentions the bytes to read from the file. readline(): Reads a single line from a file at a time. Accepts optional size parameter that mentions how many bytes to return from the file. readlines(): Returns a list of all the lines from the file.

Q.8. Given the file hfc.png, which of the following is the correct way to open the file for reading as a buffered binary file? Select all that apply.
          
            str = raw_input("Enter your input: ")
            print ("Received input is : ", str)
          
          

Select Your Option

While the bytes=True may appear to be correct, that’s not a keyword argument for the open builtin. The only way to work with byte data is to use the string value of 'b' along with the 'r' for reading or 'w' for writing. In this case we wanted to open the file for reading, so the correct mode argument here is 'rb' (read, binary).

Q.9. Appending data to a file places it at the start of the file.

Select Your Option

The given statement is false. Appending data to file places it at the end of the file.

Q.10. Which of the following mode will refer to binary data?

Select Your Option

The meanings of the modes are explained below: r - Reading w - Writing a - Appending b - Binary data + - Updating.

Q.11. Which method is used to sets the position of a file pointer

Select Your Option

A file handle or pointer denotes the position from which the file contents will be read or written. The file handle is also called a file pointer or cursor. The seek() function sets the position of a file pointer and the tell() function returns the current position of a file pointer.

Q.12. Select the correct method to delete files in Python

Select Your Option

There are 2 ways to remove a specific file: os.remove('file_path'): Removes the specified file. os.unlink('file_path'): Removes the specified file. Useful in UNIX environment.

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

Select Your Option

The writelines() method is used to write a list of strings into a file. It accepts both string and list as the argument.

Q.14. 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.

Select Your Option

write() removes all the existing content and replace it with the given contents.

Q.15. Which of the following are the modes of both writing and reading in binary format in file?

Select Your Option

The description of the mode is given below: “w” - Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “wb” - Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. “w+” - Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. “wb+” - Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

Instructions:-

  1. This Quiz is based on File Handling in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post