0% found this document useful (0 votes)
21 views6 pages

Chapter2 MCQ

Chapter 2 covers file handling in Python, including various modes for opening files, methods for reading and writing data, and error handling. It includes multiple-choice questions (MCQs) that test knowledge on file operations, such as checking if a file exists, reading lines, and using the pickle module for serialization. The chapter emphasizes understanding file modes, methods, and the implications of file operations in Python.

Uploaded by

help.akawastaken
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Chapter2 MCQ

Chapter 2 covers file handling in Python, including various modes for opening files, methods for reading and writing data, and error handling. It includes multiple-choice questions (MCQs) that test knowledge on file operations, such as checking if a file exists, reading lines, and using the pickle module for serialization. The chapter emphasizes understanding file modes, methods, and the implications of file operations in Python.

Uploaded by

help.akawastaken
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter 2 –File Handling in python

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()

21. Which of the following is an example of a text file extension?


a) .exe b) .txt c) .jpg d) .mp4

22. How are contents of a text file stored internally?


a) As plain text b) As characters only
c) As image d) As bytes consisting of 0s and 1s

23. In Python, what is the default End of Line (EOL) character?


a) \n b) \r c) \t d) \r

24. What are used to separate values in a text file?


a) Period (.) b)Semicolen(;) c) Slash (/) d) Comma (,) and tab (\t)

25. What do binary files represent?


a) ASCII characters only b) Executable, image, audio, video files
c) Only text data d) Only human-readable data

26. Why is it difficult to manually edit a binary file?


a) It contains only text b) It is compressed
c) It is not human-readable and even a small change can corrupt it d) It uses special fonts

27. What does <file.mode> return?


a) The name of the file b) The size of the file
c) The access mode in which the file was opened d) The contents of the file
28. What should be specified if the file is not in the current working directory?
a) The file extension only b) Just the filename
c) The complete path along with its name d) File creation date
29. What does the access_mode argument represent?
a) The name of the user accessing the file b) The size of the file
c) The mode in which the file is accessed by the program d) The file’s storage location

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

31. In which mode are files opened by default in Python?


a) Binary mode b) Text mode c) Append mode d) Execute mode

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

33. What is the syntax of the close() method?


a) file_object.close() b) object.close(file)
c) file.close() d) close(file_object)

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+")

36. What does the write() method in Python do?


a) Reads content from a file b) Deletes the content of the file
c) Writes a single string to a file d) Copies content from one file to another

37. What does the write() method return?


a) Nothing b) The name of the file
c) The number of characters written d) The file path

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

40. What is the purpose of the writelines() method in Python?


a) Write a single string to a file b) Write only numeric data
c) Write multiple strings to a file d) Read lines from a file

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

45. The function tell () is used to:


a) Write to a file b) Read from a file c) Tell the name of the file
d) Return the current position of the file object

46. The syntax of the tell() method is:


A) tell.file_object() B) file_object.tell()
C) file.tell(object) D) file_object:tell()

47. The function seek() is used to:


A. Search for a file B) Position the file object at a specific byte in the file
C) Move the cursor to the end of the file D) Read a file

48. What does the value 0 in seek(offset, reference_point) mean?


A) End of file B) Beginning of file C) Current position of file D) Middle
of file

49. What does the open("testfile.txt", "r+") statement do?


A) Opens the file in read-only mode B) Opens the file in write-only mode
C) Opens the file for both reading and writing D) Opens the file in append
mode

50. What is the main purpose of the Pickle module in Python?


A) To compress files B) To encrypt data
C) To serialize and deserialize Python objects D) To delete unwanted files

51. Serialization in Python refers to:


A) Reading a file B) Transforming a Python object into a byte stream
C) Converting text to HTML D) Converting strings into integers

52. What is the inverse process of serialization called?


A) Deserialization B) Decryption
C) Unpickling D) Byte conversion

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

41. What is the output of the following Python code?


d = "Hello everyone\nWriting multiline strings\nThis is the third line"
lines = d.splitlines()
print(lines)
A) ['Hello', 'everyone', 'Writing', 'multiline', 'strings', 'This', 'is', 'the', 'third', 'line']
B) ['Hello everyone Writing multiline strings This is the third line']
C) ['Hello everyone', 'Writing multiline strings', 'This is the third line']
D) ['Hello\neveryone\nWriting\nmultiline\nstrings\nThis\nis\nthe\nthird\nline']

Higher Order Thinking Skills(HOTS)


1. What will be the output of the following code if data.txt exists?
f=open(“data.txt”,”r”)
print(f.read(5))
f.close()

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

You might also like