Python MCQ Quiz Hub

File Handling in Python section 1

Choose a topic to test your knowledge and improve your Python skills

1. Which statement will read 5 characters from a file(file object ‘f’)?




2. Which function open file in python?




3. Which mode create new file if the file does not exist?




4. Which statement will return one line from a file (file object is ‘f’)?




5. readlines() method return _________




6. EOF stands for ___________




7. Which function is used to read data from Text File?




8. Which of the following will read entire content of file(file object ‘f’)?




9. Which symbol is used for append mode?




10. Which of the following options can be used to read the first line of a text file data.txt?




11. File in python is treated as sequence of ________________




12. Which function is used to write data in binary mode?




13. Which function is used to force transfer of data from buffer to file?




14. Let the file pointer is at the end of 3rd line in a text file named “data.txt”. Which of the following option can be used to read all the remaining lines?




15. __________________ module is used for serializing and de-serializing any Python object structure.




16. Which of the following error is returned when we try to open a file in write mode which does not exist?




17. _____________ function returns the strings




18. The syntax of seek() is: file_object.seek(offset [, reference_point]) What is reference_point indicate?




19. Identify the invalid mode from the following.




20. Which of the following is an invalid mode of file opening?




21. readlines( ) function returns all the words of the file in the form of List. (T/F)




22. What is ‘f’ in the following statement? f=open("Data.txt" , "r")




23. What is full form of CSV




24. Which statement will open file “data.txt” in append mode?




25. Fill in the blank import pickle f=open("data.dat",'rb') d=_____________________.load(f) f.close()




26. Which module to be imported to make the following line functional? sys.stdout.write("ABC")




27. What error is returned by the following statement if the file does not exist? f=open("A.txt")




28. Which statement will return error? import pickle f=open("data.dat",'rb') d=pickle.load(f) f.end()




29. Which of the following function takes two arguments?




30. Almost all the files in our computer stored as _______ File




31. pdf and .doc are examples of __________ files.




32. The syntax of seek() is:file_object.seek(offset [, reference_point]) What all values can be given as a reference point?




33. Fill in the blanks in the following code of writing data in binary files. Choose the answer for statement 1 import ___________ # Statement 1 rec = [ ] while True: rn = int(input("Enter")) nm = input("Enter") temp = [rn, nm] rec.append(temp) ch = input("Enter choice (Y/N)") if ch.upper == "N": break f = open("stud.dat", "____________") #statement 2 __________ .dump(rec, f) #statement 3 _______.close( ) # statement 4




34. Refer to the above code and choose the option for statement2.




35. Refer to the above code (Q 38)and choose the option for statement 3




36. Refer to the above code (Q 38)and choose the option for statement 4.




37. The syntax of seek() is:file_object.seek(offset [, reference_point] What is the default value of reference_point




38. _______ function returns the current position of file pointer. a.




39. f.seek(10,0) will move 10 bytes forward from beginning of file.




40. Which statement will move file pointer 10 bytes backward from current position.




41. When we open file in append mode the file pointer is at the _________ of the file.




42. When we open file in write mode the file pointer is at the _______ of the file.




43. Write the output of the First Print statements : f=open("data.txt",'w') f.write("Hello") f.write("Welcome to my Blog") f.close() f=open("data.txt",'r') d=f.read(5) print(d) # First Print Statement f.seek(10) d=f.read(3) print(d) # Second Print Statement f.seek(13) d=f.read(5) print(d) # Third Print Statement d=f.tell() print(d) # Fourth Print Statement




44. Refer to the above code (Q 47) : Write the output of Second Print Statement




45. Refer to the above code (Q 47) : Write the output of Third Print Statement




46. Refer to the above code (Q 47) : Write the output of Fourth Print Statement




47. A _____________ is a named location on a secondary storage media where data are permanently stored for later access




48. A _____ file consists of human readable characters.




49. Which of the following file require specific programs to access its contents?




50. Which of the following file can be opened in any text editor?




51. Each line of a text file is terminated by a special character, called the ____




52. Default EOL character in text file is _____




53. Which of the following file can be created in python?




54. In which of the following data store permanently?




55. open( ) function takes ____ as parameter.




56. Identify the correct statement to open a file:




57. Which of the following are the attributes of file handle?




58. Write the output of the following: >>> f = open("test.txt","w") >>> f.closed




59. Write the output of the following: >>> f = open("test.txt","w") >>> f.mode




60. Write the output of the following: >>> f = open("test.txt","w") >>> f.name




61. Write the output of the following: >>> f = open("test.txt","w") >>> f.close() >>> f.closed




62. Which of the following attribute of file handle returns Boolean value?




63. Ravi opened a file in python using open( ) function but forgot to specify the mode. In which mode the file will open?




64. Which of the following is invalid mode of opening file?




65. Which of the following mode will create a new file, if the file does not exist?




66. Which of the following mode will open the file in binary and read-only mode.




67. Which of the following mode will opens the file in read, write and binary mode?




68. In the given statement, the file myfile.txt will open in _______________ mode. myObject=open(“myfile.txt”, “a+”)




69. Ravi opened the file myfile.txt in append mode. In this file the file object/file handle will be at the ___




70. Ravi opened the file myfile.txt in write mode. In this file the file object/file handle will be at the __




71. Ravi opened a file in a certain mode. After opening the file, he forgot the mode. One interesting fact about that mode is ” If the file already exists, all the contents will be overwritten”. Help him to identify the correct mode.




72. Ram opened a file in a certain mode. After opening the file, he forgot the mode. The interesting facts about that mode are ” If the file doesn’t exist, then a new file will be created” and “After opening file in that mode the file handle will be at the end of the file” Help him to identify the correct mode.




73. Which of the following function is used to close the file?




74. open( ) function returns a file object called _____




75. Which of the following is the valid way to open the file?