File Handling in Python Quiz

1. What is the default mode for opening a file in Python?

  • 'r'
  • 'w'
  • 'a'
  • 'x'

2. Which method is used to read the entire file in Python?

  • file_object.read()
  • file_object.readline()
  • file_object.readlines()
  • file_object.read([n])

3. How are data stored in text files?

  • As ASCII or Unicode characters
  • In its raw memory format
  • As binary data
  • As hexadecimal values

4. What does the file_object

  • Closes the file
  • Forces the buffer to write to the file
  • Reads a line from the file
  • Writes a string to the file

5. Which mode should be used to add data to the end of an existing file?

  • 'r'
  • 'w'
  • 'a'
  • 'x'

6. How do you open a file using a relative path?

  • Using the full path from the root
  • Using a path relative to the current directory
  • Using the double backslash
  • Using an absolute path

7. What is the purpose of the with statement in file handling?

  • To open a file
  • To automatically close the file after the block of code
  • To read data from the file
  • To write data to the file

8. Which module is used for binary file operations such as dump and load?

  • sys
  • os
  • pickle
  • csv

9. How can you get the current working directory in Python?

  • import sys; sys.getcwd()
  • import os; os.getcwd()
  • import os; os.pwd()
  • import sys; sys.pwd()

10. What does file_object

  • Writes the string str to the file
  • Reads the string str from the file
  • Appends the string str to the file
  • Deletes the string str from the file

11. What is the use of EOL characters in text files?

  • To store data in raw format
  • To mark the end of a line
  • To write data to the file
  • To read data from the file

12. Which method reads all lines of a file into a list?

  • file_object.read()
  • file_object.readline()
  • file_object.readlines()
  • file_object.read([n])

13. How are binary files different from text files?

  • They store data as ASCII characters
  • They store data in its raw memory format
  • They use EOL delimiters
  • They store data as Unicode characters

14. What does the file_object

  • Reads data from the file
  • Writes data to the file
  • Ensures data is saved and file resources are released
  • Appends data to the file

15. What is the syntax to open a file for reading in Python?

  • open(filename, 'r')
  • open(filename, 'w')
  • open(filename, 'a')
  • open(filename, 'x')

16. What does the pickle

  • Writes an object to a binary file
  • Reads an object from a binary file
  • Appends an object to a binary file
  • Deletes an object from a binary file

17. What is the purpose of the sys

  • Reads from the keyboard
  • Writes errors to the monitor
  • Writes to the monitor
  • Reads from a file

18. What is the syntax to write a list of strings to a file?

  • file_object.write(str)
  • file_object.writelines(list)
  • file_object.read()
  • file_object.readlines()

19. How do you handle file paths with backslashes in Python?

  • Using single backslash
  • Using triple backslash
  • Using double backslash
  • Using quadruple backslash

20. Which file mode is used to overwrite an existing file?

  • 'r'
  • 'w'
  • 'a'
  • 'x'