File Handling in Python MCQs Exercise II


Ques 1 File Handling


How can you close a file in Python?
  a) close()
  b) exit()
  c) quit()
  d) All of the above


d is the correct option





Ques 2 File Handling


Which method is used to write data to a file in Python?
  a) read()
  b) write()
  c) append()
  d) open()


b is the correct option





Ques 3 File Handling


What does the following Python code snippet do?

file = open("filename.txt", "r")
file.seek(10)
content = file.read()
file.close()
print(content)

  a) Moves the file cursor to the 10th byte of the file and prints the content from that position
  b) Deletes the file named "filename.txt"
  c) Opens the file for writing
  d) Renames the file to "filename.txt"


a is the correct option





Ques 4 File Handling


How can you iterate over each line in a file using a for loop in Python?
  a) for line in file:
  b) for line in file.readlines():
  c) Both a and b
  d) None of the above


c is the correct option





Ques 5 File Handling


What will be the output of the following Python code snippet?

file = open("filename.txt", "r")
position = file.tell()
file.close()
print(position)

  a) Prints the contents of the file
  b) Prints the current position of the file cursor
  c) Error: File is not open for reading
  d) None of the above


b is the correct option