Chapter2 MCQ
Chapter2 MCQ
MCQs
1. What is the correct mode to open a file for reading in python?
a)”r” b)”w” c) “a” d)”x”
2. Which function is used to close an open file in python
a)file.end() b)file.close() c)file.exit() d)file.stop()
3. What will happen if you try to open a non –existent file in “r” mode?
a) A new file is created b)It opens an Emptyfile
c) It raises a FileNotFoundError d)It deletes the file
4. What mode is used to open a file for writing without deleting existing content?
a)”w” b)”a” c)”x” d)”r+”
5. What method is used to read an entire file as a string?
a)readfile() b)read() c)get() d)load()
6. How do you read a file line by line?
a)readlines() b)read() c)readline() d)Both a and c
7. What happens if a file is opened in “w” mode and alredy exists?
a) An Error occurs b) The file gets deleted and a new one is created
c) It appends new content d)It keeps the existing content and writes at the end.
8. How do you check if a file is readable?
a)file.readable() b)file.isread()
c)file.check d)file.canread()
9. What function is used to write data to a file?
a)write() b)append() c)insert() d)add()
10. What happens when a file is opened in “x”mode and already exists (exclusive creation mode)?
a) It opens normally b)It appends new element
c) It raises a FileExistsError d)it deletes the existing file and creates a new one
11.How do you delete a file in python?
a) remove() b)delete() c)erase() d)drop()
12. What library is used to handle file paths in python?
a)sys b)os c)pathlib d)shutil
13. How do you check if a file exists using python?
a) os.path.exists() b)file.exists() c)path.isfile() d)check.file()
14. What is the default mode of opening a file in python?
a)”w” b)”r” c)”a” d)”x”
15. How do you write multiple lines into a file at once?
a)writelines() b)write() c)writemultiple() d)wrtitemany()
16. How do you open a file for both reading and writing?
a)”W+” b)”r+” c)”a+” d)All of the above
17. Which statement automatically closes a file after execution?
a)with open()as file b)auto_close() c) file.close() d) safe_open()
18. How do you rename a file in python?
a)os.rename() b)file.rename() c)renamefile() d)change_name()
19. What are the two main types of files mentioned in the text?
a) Text file and code file b) Binary file and executable file
c) Text file and binary file d) ASCII file and Unicode file
20. Which Python function is used to open a file for reading or writing?
a) openfile() b) readfile() c) fileopen() d) open()
30. What does the mode <a> signify when opening a file?
a) Read-only b) Write-only c) Appending data to the end of the file
d) Deleting file contents
32. What is the purpose of the close() method in Python file handling?
a) To open a new file b) To delete the file
c) To close the file and free memory d) To rename the file
34. Which statement best describes the” with clause” in Python file handling?
a) It is used for error handling
b) It opens a file temporarily and closes it manually
c) It automatically closes the file
d) It only works with text files
35. Which of the following is the syntax using the with clause?
a) open ("file.txt", "r") as myObject b) with file("file.txt", "r"):
c) with open("myfile.txt", "r+") as myObject: d) file = open("file.txt", "r+")
38. What character should be added to create a new line in the text file using write()?
a) \n b) \\ c) \t d) /
39. What must be done before writing numeric data to a file using write()?
a) Change the file mode b) Add a newline
c) Convert the number to a string d) Use a different method
41. What is the purpose of the read() method in Python file handling?
a) To close a file b) To read a specified number of bytes from a file
c) To copy a file d) To write to a file
42. What happens when no argument is passed to the read() method?
a) Nothing is read b) Only the first line is read
c) The first 10 bytes are read d) The entire file is read
43. What is the file mode to open a file for reading in Python?
a) 'r' b) 'a' c) 'w' dds. 'x'
44. What is the output of readline(10) when the file contains “Hello everyone”?
a) Hello b) Hello every c) Hello ever d) Hello everyone
53. Which file mode is required to write (dump) data using the pickle module?
A) "w" B) "r" C) "wb" D) "rb"
54. What is the syntax for the dump() method in the pickle module?
A) dump (file_object, data_object) B) dump (data_object, file_object)
C) dump (object) D) dump (file)
55. The pickle module works with which type of files?
A) Text files B) Binary files C) CSV files D) Log files
56. What is the primary function of the dump() method in the pickle module?
A) To display Python objects B) To serialize Python objects into a binary file
C) To open binary files D) To delete data from a file
57. Which file mode should be used while load data from a binary file?
A) "w" B) "rb" C) "wb" D) "a"
58. What is the purpose of using the split() function after reading lines from a file in Python?
A) To join all lines into a single string
B) To remove newline characters from the end of each line
C) To divide each line into a list of words
D) To reverse the order of words in a line
a) Reads the first 5 lines of the file b) Reads the first 5 characters of the file
c) Reads the entire file d)Throws an error
2. What happens is the file specified in open (“file.txt”,”r”) does not exist?
a) A new file is created b) The program runs without error
c) A FileNotFoundError is raised d) The file opens in write mode
3. Which mode is used to open a file for both reading and writing?
a)”r+” b)”w” c)”a” d)“r+”
4. What will happen if you try to open an existing file in” mode?
a) Opens the file for writing without deleting its content
b) Deletes the existing content and opens the file
c) Appends data to the existing file
d) Throws an error if the file exists
5. which of the following methods is used to write data to a file in”w” mode?
a) write() b)append() c)print() d)add()
6. What is the output of the following code?
f=open(“example”,”w”)
f.write(“Hello World”)
f.close()
f=open(“example.txt”,”r”)
print(f.read())
f.close()
a) No output b) Hello c) Hello World d)FileNotFoundError