SVVN PU COLLEGE – NERALURU
Sub: CS Chapter – 2 MCQs Max.mark: 50
1. What is the primary purpose of storing data in files?
a) To execute programs faster b) To permanently store data for later access
c) To reduce memory usage d) To improve program readability
2. Where are files stored in a computer system?
a) Primary memory (RAM) b) Secondary storage media
c) CPU registers d) Cache memory
3. Which of the following is a text file extension?
a) .txt b) .jpg c) .exe d) .mp3
4. What is the main difference between text and binary files?
a) Text files are smaller in size
b) Binary files contain human-readable characters
c) Text files consist of ASCII/Unicode characters
d) Binary files cannot be opened in Python
5. What happens if a binary file is opened in a text editor?
a) It displays the correct content b) It shows garbage values
c) It automatically converts to text d) It asks for a password
6. Which of the following is an example of a binary file?
a) A .csv file b) A .py file c) A .docx d) A .txt file
7. What is the default EOL character in Python?
a) \r b) \n c) \t d) \0
8. Which encoding scheme is commonly used for text files?
a) ASCII b) JPEG c) MPEG d) ZIP
9. Which function is used to open a file in Python?
a) file_open() b) open() c) load() d) read()
10. What is the default mode for opening a file?
a) Read mode (‘r’) b) Write mode (‘w’)
c) Append mode (‘a’) d) Binary mode (‘b’)
11. Which mode is used to open a file for both reading and writing?
a) ‘r’ b) ‘w+’ c) ‘a’ d) ‘b’
12. What happens if a file opened in ‘w’ mode already exists?
a) The file is opened in read mode b) The existing content is overwritten
c) The file is deleted d) An error occurs
13. Which attribute returns the access mode of a file?
a) file.name b) file.mode c) file.closed d) file.size
14. How is a file closed in Python?
a) file.close() b) close(file) c) file.exit() d) exit(file)
15. What is the advantage of using the with clause to open a file?
a) It allows faster file operations b) It automatically closes the file
c) It encrypts the file d) It compresses the file
16. Which method is used to write a single string to a file?
a) writeline() b) write() c) append() d) insert()
17. What does the write() method return?
a) The number of characters written b) The file object
c) The content of the file d) None
18. How can numeric data be written to a text file?
a) Directly using write() b) By converting it to a string first
c) Using the dump() method d) It cannot be written
19. Which method is used to write multiple strings to a file?
a) writelines() b) write() c) appendlines() d) insertlines()
20. What is the purpose of the flush() method?
a) To close the file b) To clear the buffer and write contents to the file
c) To read the file d) To delete the file
21. Which method reads a specified number of bytes from a file?
a) read() b) readline() c) readlines() d) seek()
22. What happens if no argument is passed to the read() method?
a) It reads one line b) It reads the entire file
c) It returns an error d) It reads 10 bytes
23. Which method reads one complete line from a file?
a) read() b) readline() c) readlines() d) load()
24. What does the readlines() method return?
a) A single string b) A list of strings
c) A tuple of strings d) A dictionary of strings
25. How can you read a file line by line using a loop?
a) Using readline() in a while loop b) Using read() in a for loop
c) Using seek() in a loop d) Using dump() in a loop
26. What does the split() function do when reading a file?
a) Splits the file into multiple files b) Splits each line into a list of words
c) Joins multiple lines d) Closes the file
27. Which method returns the current position of the file object?
a) seek() b) tell() c) pos() d) offset()
28. What is the purpose of the seek() method?
a) To close the file b) To move the file object to a specific position
c) To read the file d) To write to the file
29. What is the default reference point for the seek() method?
a) Beginning of the file (0) b) Current position (1)
c) End of the file (2) d) Middle of the file
30. How do you move the file object to the 10th byte from the beginning?
a) seek(10, 0) b) seek(0, 10) c) seek(10, 1) d) seek(10, 2)
31. What happens if a file opened in ‘a’ mode does not exist?
a) An error occurs b) A new file is created
c) The file is deleted d) The file is opened in read mode
32. Which mode is used to open a file for both reading and writing without overwriting
existing content?
a) ‘r+’ b) ‘w+’ c) ‘a+’ d) ‘b+’
33. How can you iterate over all lines in a file?
a) Using a for loop on the file object b) Using a while loop with readline()
c) Both a and b d) Using seek()
34. What is the output of fileobject.tell() after writing data to a file?
a) The number of lines written b) The current byte position of the file object
c) The file size d) The number of characters written
35. What is the purpose of the pickle module?
a) To read text files b) To serialize and deserialize Python objects
c) To write binary files d) To compress files
36. Which method is used to write Python objects to a binary file?
a) write() b) dump() c) load() d) pickle()
37. Which method is used to read Python objects from a binary file?
a) read() b) load() c) get() d) unpickle()
38. What mode is used to open a binary file for writing?
a) ‘w’ b) ‘wb’ c) ‘wr’ d) ‘w+’
39. What happens if a binary file is corrupted?
a) It can be easily fixed b) It becomes unreadable
c) It automatically repairs itself d) It converts to a text file
40. Which exception is raised when the end of a binary file is reached during unpickling?
a) FileNotFoundError b) EOFError
c) IOError d) ValueError
41. What is serialization?
a) Converting Python objects to byte streams b) Reading text files
c) Writing binary files d) Closing files
42. What is deserialization?
a) Converting byte streams to Python objects b) Writing text files
c) Reading binary files d) Opening files
43. Which module is required for pickling and unpickling?
a) io b) pickle c) os d) sys
44. What is the output of file.closed if the file is open?
a) True b) False c) 1 d) 0
45. Which method is used to forcefully write buffer contents to a file?
a) close() b) flush() c) write() d) dump()
46. What is the purpose of the splitlines() method?
a) To split a file into multiple files b) To split a string into a list of lines
c) To join lines in a file d) To close a file
47. Which of the following is not a file attribute?
a) file.name b) file.mode c) file.size d) file.closed
48. What is the correct way to open a file for reading and writing in binary mode?
a) open(“file.dat”, “r+b”) b) open(“file.dat”, “rwb”)
c) open(“file.dat”, “br+”) d) open(“file.dat”, “b+r”)
49. What is the correct syntax for the with clause?
a) with open(“file.txt”, “r”) as f: b) with open(“file.txt”, “r”) -> f:
c) with open(“file.txt”, “r”) in f: d) with open(“file.txt”, “r”) f:
50. What is the purpose of the io module in Python?
a) To handle file operations b) To perform mathematical calculations
c) To create graphical interfaces d) To connect to databases