File Handling in Python – Class 12 Important MCQs with Answers & Explanations

Class 12 Computer Science – Chapter 2: File Handling in Python (200+ MCQs with Detailed Explanation)

Welcome to Mohan Exam, your trusted platform for NCERT-based and exam-oriented learning.
In this post, we bring you 200+ carefully crafted MCQs from Class 12 Computer Science – Chapter 2: File Handling in Python, designed to cover every important topic and concept of the chapter in a clear, concise, and exam-focused way.

Each question comes with a detailed explanation, helping you not only find the correct answer but also understand the logic behind it.
This MCQ series includes all key concepts — from open(), read(), and write() to advanced topics like seek(), tell(), and the pickle module — ensuring a complete and effortless understanding of Python File Handling.

With this collection, you will be able to:

  • Revise the entire NCERT syllabus in a structured way
  • Strengthen conceptual clarity through real coding examples
  • Prepare confidently for board exams and competitive tests

At Mohan Exam, our mission is simple — to make learning clear, practical, and exam-smart.
If at any point you feel there is any scope for improvement or correction, we truly welcome your valuable feedback. Your suggestions help us maintain the highest quality of educational content for every learner.

To get MCQs of all chapters of Class 12 Computer Science, visit our website Mohan Exam or click on the link below
Explore All Class 12 Computer Science MCQs on Mohan Exam


1. In Python, a file is best described as:

A. A temporary variable used for storing input
B. A named location on secondary storage media where data is permanently stored
C. A memory address for storing volatile data
D. A directory containing multiple files

Answer: B. A named location on secondary storage media where data is permanently stored

Explanation


2. What is the main limitation of storing data only in variables in a Python program?

A. Variables can store only numbers
B. Variables get deleted after program execution ends
C. Variables cannot store strings
D. Variables require file permission to access

Answer: B. Variables get deleted after program execution ends

Explanation


3. Files help in achieving which of the following?

A. Temporary memory usage
B. Data persistence and reusability
C. Faster execution
D. Only text data storage

Answer: B. Data persistence and reusability

Explanation


4. In Python, programs written in script mode are saved with which extension?

A. .py
B. .txt
C. .docx
D. .csv

Answer: A. .py

Explanation


5. How are files stored on a computer internally?

A. As a sequence of characters
B. As a sequence of bytes (0s and 1s)
C. As text paragraphs
D. As encoded audio signals

Answer: B. As a sequence of bytes (0s and 1s)

Explanation


6. What are the two main types of data files in Python?

A. Text file and Binary file
B. Audio file and Video file
C. CSV file and DOC file
D. HTML file and XML file

Answer: A. Text file and Binary file

Explanation


7. Which of the following statements about text files is correct?

A. They contain non-human readable data
B. They can be opened by any text editor like Notepad
C. They require special programs to open
D. They store images and audio

Answer: B. They can be opened by any text editor like Notepad

Explanation


8. Which of the following file extensions generally represent text files?

A. .exe, .dll
B. .jpg, .png
C. .txt, .py
D. .bin, .dat

Correct Answer: C. .txt, .py

Explanation


9. Binary files are different from text files because:

A. They can be read by any editor
B. They are stored in human-readable characters
C. They are made up of symbols and non-human-readable data
D. They always contain ASCII text

Answer: C. They are made up of symbols and non-human-readable data

Explanation


10. When we open a text file, what actually happens internally?

A. Python converts text into Unicode values stored as bytes
B. The file is stored temporarily in cache
C. Each character is converted to hexadecimal values
D. The file is copied to memory in plain text format

Answer: A. Python converts text into Unicode values stored as bytes

Explanation


11. In computing, how is every file stored internally on a computer?

A. As a list of characters
B. As a sequence of bytes (0s and 1s)
C. As text paragraphs
D. As Unicode strings

Answer: B. As a sequence of bytes (0s and 1s)

Explanation


12. Which of the following statements correctly differentiates between text files and binary files?

A. Both contain human-readable characters
B. Text files contain human-readable data, while binary files contain non-human-readable data
C. Binary files can be opened in Notepad
D. Text files always require special software to access

Answer: B. Text files contain human-readable data, while binary files contain non-human-readable data

Explanation


13. Which of the following extensions are commonly used for text files in Python?

A. .jpg, .png
B. .txt, .py, .csv
C. .exe, .dll
D. .bin, .dat

Answer: B. .txt, .py, .csv

Explanation


14. When a text file is opened using a text editor, how does it display readable text?

A. The text editor converts ASCII values into human-readable characters
B. The text editor executes the binary data directly
C. The text editor removes all non-printable bytes
D. The text editor compresses the data for display

Answer: A. The text editor converts ASCII values into human-readable characters

Explanation


15. In ASCII encoding, which number represents the capital letter ‘A’?

A. 60
B. 65
C. 70
D. 97

Answer: B. 65

Explanation


16. What is the purpose of the End of Line (EOL) character in a text file?

A. To indicate the end of the file
B. To represent a space
C. To mark the end of a line and start a new one
D. To separate different file formats

Answer: C. To mark the end of a line and start a new one

Explanation


17. What is the default End of Line (EOL) character used in Python text files?

A. \t
B. \n
C. \s
D. \\

Answer: B. \n

Explanation


18. Besides whitespace, which characters are commonly used to separate values in text files?

A. Comma ( , ) and tab ( \t )
B. Dot ( . ) and semicolon ( ; )
C. Colon ( : ) and dash ( – )
D. Slash ( / ) and backslash ( \ )

Answer: A. Comma ( , ) and tab ( \t )

Explanation


19. How is a text file different from a .docx file?

A. A text file stores only ASCII data, whereas a .docx file includes formatting and metadata
B. A .docx file is smaller in size than a text file
C. Both store data only in binary symbols
D. A text file cannot be opened by Notepad

Answer: A. A text file stores only ASCII data, whereas a .docx file includes formatting and metadata

Explanation


20. When comparing the file sizes of a .txt file and a .docx file containing the same text, what will you observe?

A. Both have the same size
B. The .txt file will be larger
C. The .docx file will be significantly larger
D. The .docx file will be smaller due to compression

Answer: C. The .docx file will be significantly larger

Explanation


21. Which of the following statements about binary files is TRUE?

A. They store only ASCII characters
B. They contain human-readable text
C. They store data as bytes representing actual content like images or videos
D. They can be opened with any text editor

Answer: C. They store data as bytes representing actual content like images or videos

Explanation


22. What happens if we try to open a binary file using a text editor?

A. The file displays normally
B. The editor automatically converts it into text
C. It shows unreadable garbage characters
D. It will not open at all

Answer: C. It shows unreadable garbage characters

Explanation


23. A small change in a binary file, such as one bit flipped from 0 to 1, can cause:

A. No effect on file content
B. The file to become unreadable or corrupted
C. The file size to increase
D. The system to delete the file

Answer: B. The file to become unreadable or corrupted

Explanation


Mohan Exam

24. Why is it difficult to correct errors in binary files manually?

A. Because they use a compressed format
B. Because the data is not stored sequentially
C. Because the content is not human-readable
D. Because Python doesn’t support binary editing

Answer: C. Because the content is not human-readable

Explanation


25. Which module in Python provides functions to handle files such as reading and writing?

A. sys
B. os
C. io
D. file

Answer: C. io

Explanation


26. What is the primary purpose of the open() function in Python?

A. To import the io module
B. To connect a Python program with a data file
C. To print data from the file
D. To rename a file

Answer: B. To connect a Python program with a data file

Explanation


27. The correct syntax of the open() function is:

A. file = open(filename)
B. file = open(filename, access_mode)
C. open.file(filename, mode)
D. open(filename, mode, variable)

Answer: B. file = open(filename, access_mode)

Explanation


28. What does the open() function return after successfully opening a file?

A. A Boolean value
B. A string containing file path
C. A file object (file handle)
D. The number of bytes in the file

Answer: C. A file object (file handle)

Explanation


29. If the file mentioned in the open() function does not exist, what happens?

A. The program stops with an error (if mode is 'r')
B. A new empty file is automatically created (if mode is 'w', 'a', or 'x')
C. The system shuts down
D. Python ignores the command

Answer: B. A new empty file is automatically created

Explanation


30. The attribute <file.closed> in Python is used to:

A. Display file contents
B. Return True if the file is closed, else False
C. Delete a file from memory
D. Check file permissions

Answer: B. Return True if the file is closed, else False

Explanation


31. Binary files are stored in terms of:

A. Characters
B. ASCII values
C. Bytes
D. Unicode

Answer: C. Bytes

Explanation


32. What happens if you try to open a binary file using a text editor?

A. The file opens normally
B. The text editor automatically converts it
C. The file displays garbage values
D. The file gets deleted

Answer: C. The file displays garbage values

Explanation


33. Which module in Python is used for handling files?

A. os
B. sys
C. io
D. file

Answer: C. io

Explanation


34. What does the function open(filename, mode) return in Python?

A. File name only
B. File handle or file object
C. File size
D. File extension

Answer: B. File handle or file object

Explanation


35. What does file.closed return?

A. File name
B. File path
C. True if file is closed, else False
D. File size

Answer: C. True if file is closed, else False

Explanation


36. What does file.mode return?

A. File’s data type
B. Access mode used while opening the file
C. File name
D. File offset position

Answer: B. Access mode used while opening the file

Explanation


37. Which of the following opens a file in read-only mode?

A. 'r'
B. 'w'
C. 'a'
D. 'r+'

Answer: A. 'r'

Explanation


38. Which of these modes opens a file for writing in binary mode, overwriting its contents?

A. 'wb'
B. 'rb'
C. 'ab'
D. 'r+'

Answer: A. 'wb'

Explanation


39. If you open a file using mode 'ab+', what happens?

A. File opens at the beginning for reading and writing
B. File opens at the end for appending and reading
C. File content gets deleted
D. File is opened only for reading

Answer: B. File opens at the end for appending and reading

Explanation


40. In which mode does the file pointer start at the end of the file?

A. 'r'
B. 'a'
C. 'w'
D. 'rb'

Answer: B. 'a'

Explanation


41. Which access mode creates a new file if it does not exist and overwrites if it does?

A. 'r'
B. 'a'
C. 'w'
D. 'rb'

Answer: C. 'w'

Explanation


42. What is the default mode of open() function if not specified?

A. 'r'
B. 'w'
C. 'a'
D. 'rb'

Answer: A. 'r'

Explanation


43. What does the following code do?

f = open("data.bin", "rb+")

A. Opens file for read only
B. Opens file for binary read and write
C. Opens file for append
D. Creates file if not exist

Answer: B. Opens file for binary read and write

Explanation


44. Which mode allows reading and writing in binary, overwriting existing content?

A. 'rb+'
B. 'wb+'
C. 'ab+'
D. 'r+'

Answer: B. 'wb+'

Explanation


45. Which attribute returns the name of the file opened using open() function?

A. file.name
B. file.title
C. file.path
D. file.mode

Answer: A. file.name

Explanation


46. Which method in Python is used to close a file after read/write operations?

A. end()
B. stop()
C. close()
D. terminate()

Answer: C. close()

Explanation


47. What happens when a file is closed using close() method?

A. File gets deleted
B. File becomes inaccessible until reopened
C. File content is erased
D. The program terminates

Answer: B. File becomes inaccessible until reopened

Explanation


48. What is the main advantage of closing a file after use?

A. Prevents file renaming
B. Saves memory and writes unsaved data
C. Automatically changes file mode
D. Encrypts the file content

Answer: B. Saves memory and writes unsaved data

Explanation


49. What will happen if a file object is reassigned to another file before closing the previous one?

A. Python throws an error
B. Both files remain open
C. The previous file is automatically closed
D. The previous file’s data is lost

Answer: B. Both files remain open

Explanation


50. What is the purpose of the with clause in file handling?

A. To open multiple files simultaneously
B. To automatically close the file after its block execution
C. To delay writing operations
D. To rename the file after writing

Answer: B. To automatically close the file after its block execution

Explanation


51. Choose the correct syntax of opening a file using the with clause.

A. with open(file_name, mode):
B. open with(file_name, mode):
C. with file_name.open(mode):
D. file open with(mode):

Answer: A. with open(file_name, mode):

Explanation


52. Which of the following statements is true regarding the with clause?

A. The file must be closed manually
B. It closes the file automatically after the block is executed
C. It keeps the file open permanently
D. It can only be used with binary files

Answer: B. It closes the file automatically after the block is executed

Explanation


53. In the statement

with open("myfile.txt", "r+") as myObject:
    content = myObject.read()

What is the mode used here?

A. Append mode
B. Read-only mode
C. Read and Write mode
D. Write-only mode

Answer: C. Read and Write mode

Explanation


54. What happens if a file is opened in write mode (‘w’) and the file already exists?

A. The file is appended
B. An error occurs
C. File is deleted permanently
D. Its contents are erased before writing new data

Answer: D. Its contents are erased before writing new data

Explanation


55. When a file is opened in append mode (‘a’), where is the file pointer positioned?

A. Beginning of file
B. Middle of file
C. End of file
D. Random position

Answer: C. End of file

Explanation


56. Which of the following modes allows both reading and appending in a file?

A. 'w+'
B. 'a+'
C. 'r+'
D. 'rb'

Answer: B. 'a+'

Explanation


57. Which of the following statements about with clause is incorrect?

A. It automatically closes the file
B. It requires manual closing
C. It prevents memory leakage
D. It simplifies code readability

Answer: B. It requires manual closing

Explanation


58. The following code:

myObject = open("data.txt", "a+")

opens the file for:

A. Reading only
B. Writing only
C. Reading and appending
D. Writing and truncating

Answer: C. Reading and appending

Explanation


59. Why is the with statement preferred over using open() and close() separately?

A. It consumes more memory
B. It reduces syntax errors and ensures safe closure
C. It only works in Python 3.10+
D. It writes faster

Answer: B. It reduces syntax errors and ensures safe closure

Explanation


60. What will happen if an exception occurs inside a with block?

A. File remains open
B. File closes automatically
C. File gets deleted
D. File becomes read-only

Answer: B. File closes automatically

Explanation


61. Which method in Python is used to write a single string into a text file?


A. write()
B. writelines()
C. read()
D. append()

Answer: A. write()

Explanation


62. The write() method returns:

A. Nothing
B. Boolean value
C. The number of characters written
D. The file name

Answer: C. The number of characters written

Explanation


63. What is the output of the following code?

myfile = open("demo.txt", "w")
x = myfile.write("Python\nFile Handling")
print(x)
myfile.close()

A. 0
B. 19
C. 20
D. 21

Answer: C. 20

Explanation


64. In the statement myfile.write("Hello\nWorld"), the \n represents:

A. Null value
B. Tab space
C. End of line
D. End of file

Answer: C. End of line (newline)

Explanation

65. What will happen if you use the write() method to write numeric data directly?

A. It will be written as integer
B. It will raise a TypeError
C. It will convert automatically
D. It will write ASCII value

Answer: B. It will raise a TypeError

Explanation


Mohan Exam

66. Which of the following statements correctly writes a numeric value into a file?

A. myfile.write(123)
B. myfile.write(str(123))
C. myfile.writelines(123)
D. myfile.write(int(123))

Answer: B. myfile.write(str(123))

Explanation


67. The flush() method in Python is used to:

A. Erase file data
B. Close the file automatically
C. Write buffer contents to the file immediately
D. Move file pointer to start

Answer: C. Write buffer contents to the file immediately

Explanation


68. Which of the following correctly writes multiple strings to a file in one go?

A. write(["A","B","C"])
B. writelines(["A","B","C"])
C. write("A","B","C")
D. write(list("ABC"))

Answer: B. writelines(["A","B","C"])

Explanation


69. What is the difference between write() and writelines()?

A. write() writes multiple lines; writelines() only one
B. write() accepts string; writelines() accepts iterable of strings
C. Both are identical
D. write() is faster

Answer: B. write() accepts string; writelines() accepts iterable of strings

Explanation


70. If we open a file in write mode (‘w’) and call write() twice, what will happen?

A. Only the first write is saved
B. Only the last write is saved
C. Both will be written sequentially
D. File will raise an error

Answer: C. Both will be written sequentially

Explanation


71. What is the correct statement about buffer in Python file handling?

A. Buffer stores data temporarily before writing to disk
B. Buffer prevents data loss
C. Data is written directly to the disk
D. Buffer is not used in file handling

Answer: A. Buffer stores data temporarily before writing to disk

Explanation


72. In which situation is it necessary to use flush() manually?

A. When working with large files that must update frequently
B. When file is read-only
C. When using with statement
D. When file is already closed

Answer: A. When working with large files that must update frequently

Explanation


73. What does the following code do?

data = ["Line1\n", "Line2\n", "Line3\n"]
myfile = open("info.txt", "w")
myfile.writelines(data)
myfile.close()

A. Writes only the first string
B. Writes all three strings to info.txt
C. Produces a TypeError
D. Writes nothing

Answer: B. Writes all three strings to info.txt

Explanation


74. Which statement about writelines() is incorrect?

A. It does not add newline automatically
B. It writes multiple strings from an iterable
C. It adds \n after each element automatically
D. Each string must already contain newline if desired

Answer: C. It adds \n after each element automatically

Explanation


75. After calling write(), data is first stored in:

A. RAM buffer
B. Hard disk directly
C. Stack memory
D. Output window

Answer: A. RAM buffer

Explanation


76. If a file is opened using 'a' mode and we write "Hello", then close it and reopen in 'r' mode — what happens?

A. File will be empty
B. Old data is erased
C. "Hello" will appear at the end of old content
D. File will show an error

Answer: C. "Hello" will appear at the end of old content

Explanation


77. Which of the following is true about write() method?

A. It accepts numeric and string data both
B. It must be followed by flush()
C. It accepts only string arguments
D. It writes data line-by-line automatically

Answer: C. It accepts only string arguments

Explanation


78. What will happen if we forget to close a file after using write()?

A. Data might remain unsaved in buffer
B. File automatically deletes
C. Python raises runtime error
D. File gets locked permanently

Answer: A. Data might remain unsaved in buffer

Explanation


79. The newline character \n in Python counts as how many characters when written to a file?

A. 0
B. 1
C. 2
D. Depends on OS

Answer: B. 1

Explanation


80. Which statement best describes the role of write() and writelines()?

A. Both read file data
B. Both write data to files
C. Both delete file data
D. Both close files

Answer: B. Both write data to files

Explanation


81. What does the writelines() method return?

A. Number of characters written
B. List of strings written
C. None
D. Boolean value

Answer: C. None

Explanation


82. Which of the following statements is true about writelines()?

A. It adds a newline after each string automatically
B. It can write multiple strings from an iterable
C. It returns the length of the iterable
D. It can only write one string

Answer: B. It can write multiple strings from an iterable

Explanation


83. What will happen if a tuple of numbers is passed to writelines()?

A. Numbers are written as strings automatically
B. An error is raised
C. Only the first number is written
D. Numbers are converted to ASCII characters

Answer: B. An error is raised

Explanation


84. What modes must a file be opened in to read its contents?

A. 'r', 'r+', 'w+', 'a+'
B. 'w', 'wb'
C. 'a' only
D. 'wb+' only

Answer: A. 'r', 'r+', 'w+', 'a+'

Explanation


85. What does the read(n) method do?

A. Reads the whole file irrespective of n
B. Reads n bytes from the file
C. Writes n bytes to the file
D. Deletes first n bytes from the file

Answer: B. Reads n bytes from the file

Explanation


86. What is the output of the following code?

myfile = open("myfile.txt", "r")
print(myfile.read(10))
myfile.close()

If the file contains "Hello everyone".

A. Hello
B. Hello ever
C. Hello everyone
D. Error

Answer: B. Hello ever

Explanation


87. What happens if read() is called with no arguments or a negative number?

A. Reads only the first line
B. Reads entire file content
C. Reads zero characters
D. Raises error

Answer: B. Reads entire file content

Explanation


88. Which of the following statements is correct?

A. writelines() adds newline characters automatically
B. read(n) can read negative characters
C. read() reads entire file if no argument is given
D. write() cannot write strings

Answer: C. read() reads entire file if no argument is given

Explanation


89. What is the correct way to read an entire file after opening it in 'r' mode?

A. file.read(0)
B. file.read(-1)
C. file.readlines(0)
D. file.read()

Answer: D. file.read()

Explanation


90. Consider the following code:

lines = ["Hello everyone\n", "Writing multiline strings\n", "This is the third line\n"]
myfile = open("myfile.txt", 'w')
myfile.writelines(lines)
myfile.close()

What will appear in myfile.txt when opened in Notepad?

A. All strings concatenated without line breaks
B. Each string on a separate line
C. Only the first line
D. Error

Answer: B. Each string on a separate line

Explanation


91. What will happen if the \n is omitted from strings in writelines()?

A. Strings will be written on the same line
B. Strings will be automatically separated
C. File will raise an error
D. Only first string will be written

Answer: A. Strings will be written on the same line

Explanation

Explanation:
writelines() does not add newlines. Without \n, all strings are written consecutively on a single line.


92. Which of the following modes allows reading after appending data to a file?

A. 'w'
B. 'a'
C. 'a+'
D. 'wb'

Answer: C. 'a+'

Explanation


93. Which of the following is true about read() and read(n)?

A. read(n) returns a list of strings
B. read() reads entire file, read(n) reads n bytes
C. read() cannot read text files
D. read(n) writes n bytes

Answer: B. read() reads entire file, read(n) reads n bytes

Explanation


94. Which of the following is incorrect?

A. read() moves the file pointer
B. writelines() returns number of characters
C. read(n) reads n characters
D. writelines() can write multiple strings

Answer: B. writelines() returns number of characters

Explanation


95. What happens if a file is opened in 'r' mode but does not exist?

A. File is created
B. File pointer is at end
C. Raises FileNotFoundError
D. File is empty

Answer: C. Raises FileNotFoundError

Explanation


96. What does readline(n) do in Python?

A. Reads entire file
B. Reads first n characters of the first line (up to newline)
C. Reads n lines from the file
D. Writes n characters to the file

Answer: B. Reads first n characters of the first line (up to newline)

Explanation


97. What does readline() without any argument return?

A. Nothing
B. One complete line including the newline character
C. Entire file content
D. List of strings

Answer: B. One complete line including the newline character

Explanation


98. What is returned by readline() when EOF (End of File) is reached?

A. 'EOF'
B. Empty string ''
C. None
D. Raises error

Answer: B. Empty string ''

Explanation


99. Consider the following code:

myfile = open("myfile.txt", "r")
print(myfile.readline(10))
myfile.close()

If the first line of myfile.txt is "Hello everyone\n", the output will be:

A. Hello ever
B. Hello everyon
C. Hello everyone
D. Hello

Answer: A. Hello ever

Explanation


100. Which of the following is correct about iterating over a file object using readline()?

A. Returns a list of lines
B. Returns one line at a time
C. Reads entire file at once
D. Writes lines to another file

Answer: B. Returns one line at a time

Explanation


101. What does the readlines() method return?

A. Entire file as a single string
B. A list of strings where each element is a line
C. One line at a time
D. Number of lines in the file

Answer: B. A list of strings where each element is a line

Explanation


102. Which of the following is true?

A. readlines() automatically removes \n
B. readlines() returns strings including newline characters
C. readlines() returns integers
D. readlines() can write data

Answer: B. readlines() returns strings including newline characters

Explanation


103. What will the following code output?

myfile = open("myfile.txt", "r")
lines = myfile.readlines()
print(lines)
myfile.close()

If myfile.txt contains:

Hello
Python
File Handling

A. 'Hello\nPython\nFile Handling\n'
B. ['Hello\n', 'Python\n', 'File Handling\n']
C. ['Hello', 'Python', 'File Handling']
D. Hello Python File Handling

Answer: B. ['Hello\n', 'Python\n', 'File Handling\n']

Explanation


104. Which method would you use to read a file line by line without loading the entire file into memory?

A. read()
B. readline()
C. readlines()
D. write()

Answer: B. readline()

Explanation


105. Which of the following statements about readline(n) is correct?

A. It reads exactly n lines
B. It reads up to n characters of the current line
C. It reads the whole file if n is negative
D. It writes n characters

Answer: B. It reads up to n characters of the current line

Explanation


106. What happens if readlines() is called on an empty file?

A. Returns None
B. Returns an empty list []
C. Raises FileNotFoundError
D. Returns a list with empty string [”]

Answer: B. Returns an empty list []

Explanation


107. Which of the following is false about readline()?

A. Reads one line at a time
B. Stops at newline character
C. Returns empty string at EOF
D. Returns a list of lines

Answer: D. Returns a list of lines

Explanation


108. What is a common use of split() after readlines()?

A. To convert each line to a number
B. To display each word separately as list elements
C. To remove newline characters automatically
D. To reverse file contents

Answer: B. To display each word separately as list elements

Explanation


109. How can you iterate over a file object to read lines without readline() or readlines()?

A. Using a for loop directly on the file object
B. Using write()
C. Using close()
D. Using append()

Answer: A. Using a for loop directly on the file object

Explanation


110. Which of the following will not preserve the newline character?

A. readlines()
B. readline()
C. splitlines()
D. Iterating with for line in file:

Answer: C. splitlines()

Explanation


111. What does the split() method do when used on a line read from a file?

A. Splits the line into characters
B. Splits the line into words based on whitespace
C. Splits the file into lines
D. Removes newline characters

Answer: B. Splits the line into words based on whitespace

Explanation


112. Consider the following code:

for line in d:
    words = line.split()
    print(words)

If d contains:

Hello everyone
Writing multiline strings
This is the third line

What will be the output of the first iteration?

A. ['Hello', 'everyone']
B. ['Hello everyone']
C. 'Hello'
D. ['H','e','l','l','o']

Answer: A. ['Hello', 'everyone']

Explanation


113. What is the difference between split() and splitlines()?

A. split() splits lines into words, splitlines() splits file into lines
B. split() removes newline, splitlines() keeps newline
C. Both behave the same
D. splitlines() splits line into words

Answer: A. split() splits lines into words, splitlines() splits file into lines

Explanation


114. What will be the output of splitlines() on the line "Hello everyone\n"?

A. ['Hello', 'everyone']
B. ['Hello everyone']
C. 'Hello everyone'
D. ['H','e','l','l','o']

Answer: B. ['Hello everyone']

Explanation


115. Which mode is used to write user input into a text file in Program 2-1?

A. 'r'
B. 'w'
C. 'a'
D. 'rb'

Answer: B. 'w'

Explanation


116. What will happen if the file testfile.txt already exists in Program 2-1?

A. It will append the new input
B. It will overwrite existing content
C. It will raise an error
D. It will read existing content first

Answer: B. It will overwrite existing content

Explanation


117. In Program 2-1, which method writes the user input to the file?

A. read()
B. write()
C. readlines()
D. split()

Answer: B. write()

Explanation


118. In Program 2-1, how is the file read after writing?

A. Using read()
B. Using for str in fobject: loop
C. Using writelines()
D. Using split()

Answer: B. Using for str in fobject: loop

Explanation


119. Why is fobject.close() used after writing to the file?

A. To delete the file
B. To free system resources and flush buffer
C. To append more data
D. To read the file

Answer: B. To free system resources and flush buffer

Explanation


120. What will happen if close() is not called after writing in Program 2-1?

A. Data may not be written to file completely
B. File will be deleted automatically
C. File pointer moves to beginning
D. Python raises error

Answer: A. Data may not be written to file completely

Explanation


Mohan Exam

121. Which of the following correctly describes the flow of Program 2-1?

A. Read file → Write user input → Close file
B. Write user input → Close file → Open for reading → Iterate lines
C. Open file → Append → Read → Split lines
D. Close file → Write → Read

Answer: B. Write user input → Close file → Open for reading → Iterate lines

Explanation


122. What type of object is returned by open()?

A. String
B. File object
C. List
D. Integer

Answer: B. File object

Explanation


123. What will for str in fobject: do in Program 2-1?

A. Iterate over words
B. Iterate over lines
C. Iterate over characters
D. Split lines automatically

Answer: B. Iterate over lines

Explanation


124. What happens if input() is empty in Program 2-1?

A. Writes a blank line to the file
B. Raises error
C. Does not create the file
D. Writes default string

Answer: A. Writes a blank line to the file

Explanation


125. Which of the following can be used to display each word separately from a line read from the file?

A. splitlines()
B. split()
C. readlines()
D. write()

Answer: B. split()

Explanation


126. What does tell() method return?

A. Number of lines in a file
B. Current byte position of the file object
C. Total size of the file
D. File name

Answer: B. Current byte position of the file object

Explanation


127. Which of the following is correct about seek(offset, reference_point)?

A. Moves the file pointer to a specific line number
B. Moves the file pointer by offset from the reference point
C. Writes data at the specified position
D. Deletes bytes at the specified offset

Answer: B. Moves the file pointer by offset from the reference point

Explanation


128. What is the default value of the reference_point in seek()?

A. 0 (beginning of file)
B. 1 (current position)
C. 2 (end of file)
D. None

Answer: A. 0 (beginning of file)

Explanation


129. In the statement fileObject.seek(5,0), what does 5 signify?

A. Move 5 bytes from current position
B. Move 5 bytes from end of file
C. Move 5 bytes from beginning of file
D. Move 5 lines from beginning

Answer: C. Move 5 bytes from beginning of file

Explanation


130. Which reference_point value moves the pointer relative to the current file position?

A. 0
B. 1
C. 2
D. None

Answer: B. 1

Explanation


131. Which reference_point value moves the pointer relative to the end of file?

A. 0
B. 1
C. 2
D. -1

Answer: C. 2

Explanation


132. Does seek() work the same for text and binary files?

A. Yes
B. No
C. Only in read mode
D. Only in write mode

Answer: B. No

Explanation
“In text files, only offsets returned by tell() are legal arguments to seek(). The whence value should be 0.”


133. What is the output of the following code?

f = open("testfile.txt", "r")
print(f.tell())
f.read(5)
print(f.tell())
f.close()

If the file contains "Hello Python", what will be the output?

A. 0 5
B. 0 11
C. 5 10
D. 1 6

Answer: A. 0 5

Explanation


134. Which method would you use to read a file randomly instead of sequentially?

A. read()
B. readline()
C. seek()
D. write()

Answer: C. seek()

Explanation


135. If seek(0,2) is executed on a file, what does it do?

A. Moves pointer to start
B. Moves pointer to current position
C. Moves pointer to end
D. Moves pointer to 2nd byte

Answer: C. Moves pointer to end

Explanation


136. Why do we need tell() method?

A. To know file size
B. To know current position of file pointer
C. To write data at beginning
D. To close the file

Answer: B. To know current position of file pointer

Explanation


137. What will happen if you execute seek(-3,1)?

A. Moves pointer 3 bytes back from current position
B. Moves pointer to beginning
C. Moves pointer 3 bytes forward from end
D. Raises error

Answer: A. Moves pointer 3 bytes back from current position

Explanation


138. Consider binary files. Why is seek() preferred over reading sequentially?

A. Faster access to specific data
B. Required for all reads
C. Writes data automatically
D. Deletes bytes automatically

Answer: A. Faster access to specific data

Explanation


139. Which of the following is true about seek() and tell()?

A. seek() returns current position
B. tell() moves pointer
C. seek() moves pointer, tell() returns pointer position
D. Both write data

Answer: C. seek() moves pointer, tell() returns pointer position

Explanation


140. What is the main limitation of using seek() in text files?

A. Cannot read content
B. Byte positions may not match character positions due to encoding
C. Cannot write data
D. Only works in append mode

Answer: B. Byte positions may not match character positions due to encoding

Explanation


141. What does the following statement do?

fileobject = open("testfile.txt","r+")

A. Opens the file for reading only
B. Opens the file for reading and writing
C. Opens the file in append mode
D. Opens the file as a binary file

Answer: B. Opens the file for reading and writing

Explanation


142. In Program 2-2, what does fileobject.tell() return initially?

A. 10
B. 0
C. Length of file
D. -1

Answer: B. 0

Explanation


143. What will fileobject.seek(0) do?

A. Move pointer to 0th byte from beginning
B. Move pointer 0 bytes forward from current position
C. Move pointer to end of file
D. Overwrite first byte

Answer: A. Move pointer to 0th byte from beginning

Explanation


144. In Program 2-2, after fileobject.seek(10), what does fileobject.read() do?

A. Reads first 10 bytes
B. Reads all bytes starting from 10th byte
C. Reads last 10 bytes
D. Reads the whole file again

Answer: B. Reads all bytes starting from 10th byte

Explanation


145. What is the output of fileobject.tell() after fileobject.seek(10)?

A. 0
B. 10
C. 5
D. Length of file

Answer: B. 10

Explanation


146. Which mode should be used to create a new file and overwrite existing contents?

A. 'r'
B. 'w'
C. 'a'
D. 'r+'

Answer: B. 'w'

Explanation


147. Which mode should be used to add data at the end of an existing file without deleting previous content?

A. 'w'
B. 'r+'
C. 'a'
D. 'rb'

Answer: C. 'a'

Explanation


148. What is the main difference between write and append modes?

A. Write mode reads the file, append mode does not
B. Write mode overwrites file, append mode preserves existing data
C. Append mode deletes file
D. Both behave the same

Answer: B. Write mode overwrites file, append mode preserves existing data

Explanation


149. In file traversal, which Python construct is commonly used to read a file line by line?

A. while with read()
B. for loop on file object
C. readlines() without loop
D. write()

Answer: B. for loop on file object

Explanation


150. After creating a file with open("practice.txt","w"), what will happen if write() is not called?

A. File remains empty
B. File is deleted
C. Python raises error
D. File contains default text

Answer: A. File remains empty

Explanation


151. Which of the following is TRUE about r+ mode?

A. Creates new file if it doesn’t exist
B. Pointer starts at end of file
C. Allows both reading and writing, file must exist
D. Always overwrites file

Answer: C. Allows both reading and writing, file must exist

Explanation


152. What does fileobject.read() return after seek() moves pointer to a specific position?

A. Entire file content
B. Content starting from pointer position to EOF
C. Only the byte at pointer
D. Nothing

Answer: B. Content starting from pointer position to EOF

Explanation


153. Which of the following sequences correctly represents Program 2-2 execution?

A. Open file → read → tell → seek → read → close
B. Open file → write → seek → read → close
C. Open file → append → tell → write → close
D. Open file → readlines → split → close

Answer: A. Open file → read → tell → seek → read → close

Explanation


154. What happens if you use seek(10) on a file with less than 10 bytes?

A. Raises error
B. Pointer moves to EOF
C. Overwrites file
D. File is deleted

Answer: B. Pointer moves to EOF

Explanation


155. Why is it important to close a file after seek and read operations?

A. To reset pointer
B. To flush buffer and release system resources
C. To delete file
D. To move pointer automatically

Answer: B. To flush buffer and release system resources

Explanation


156. What does open("practice.txt", "w+") do?

A. Opens the file for reading only
B. Opens the file for reading and writing; overwrites if file exists
C. Opens file in append mode
D. Opens file as binary

Answer: B. Opens the file for reading and writing; overwrites if file exists

Explanation


157. What happens if a file is opened in 'a' mode and new data is written?

A. Previous data is deleted
B. New data is written after existing content
C. File is read-only
D. Error occurs

Answer: B. New data is written after existing content

Explanation


158. In Program 2-3, which loop allows the user to enter multiple lines interactively?

A. for loop
B. while loop
C. do-while loop
D. None

Answer: B. while loop

Explanation


159. In Program 2-3, what happens if the user enters 'n' when asked “Do you wish to enter more data?”

A. Next line is overwritten
B. Loop breaks and file is closed
C. File is deleted
D. Error occurs

Answer: B. Loop breaks and file is closed

Explanation


160. Which mode should be used to read a file line by line?

A. 'w'
B. 'r'
C. 'a'
D. 'w+'

Answer: B. 'r'

Explanation


161. In Program 2-4, which method is used to read a single line from the file?

A. read()
B. readline()
C. readlines()
D. write()

Answer: B. readline()

Explanation


162. What is the termination condition for the while loop in Program 2-4?

A. EOF reached (readline() returns empty string)
B. User input 'n'
C. File pointer at 0
D. None

Answer: A. EOF reached (readline() returns empty string)

Explanation


163. What happens if you use readlines() instead of readline() in Program 2-4?

A. Reads one line at a time
B. Reads all lines into a list
C. Overwrites file
D. Deletes EOF

Answer: B. Reads all lines into a list

Explanation


164. Which of the following is TRUE about ‘w+‘ and ‘a+‘ modes?

A. w+ appends, a+ overwrites
B. w+ overwrites, a+ appends
C. Both append
D. Both overwrite

Answer: B. w+ overwrites, a+ appends

Explanation


165. What will happen if fileobject.close() is not called after writing?

A. Data may not be saved to disk
B. File is deleted automatically
C. File pointer resets to 0
D. Nothing happens

Answer: A. Data may not be saved to disk

Explanation


166. In Program 2-3, which statement ensures user input is written to the file?

A. data = input(...)
B. fileobject.write(data)
C. fileobject.close()
D. break

Answer: B. fileobject.write(data)

Explanation


167. How does Program 2-4 traverse the entire file?

A. Using for line in fileobject
B. Using while str: with readline()
C. Using read() in a loop
D. Using seek() only

Answer: B. Using while str: with readline()

Explanation


168. Which of the following is a correct sequence to write and then display a file?

A. Open → traverse → write → close
B. Open in write mode → write → close → open in read mode → traverse → close
C. Open → read → write → close
D. Open → append → read → overwrite → close

Answer: B. Open in write mode → write → close → open in read mode → traverse → close

Explanation


169. What will happen if user enters empty string in Program 2-3?

A. Nothing is written
B. File is deleted
C. Empty line is written
D. Error occurs

Answer: C. Empty line is written

Explanation


170. Why is it recommended to use ‘w+‘ instead of ‘w‘ sometimes?

A. Allows reading after writing
B. Appends data
C. Deletes old file
D. Only works in binary

Answer: A. Allows reading after writing

Explanation


171. What is the purpose of opening a file in 'w+' mode?

A. Read-only
B. Write-only
C. Read and write; overwrites if file exists
D. Append-only

Answer: C. Read and write; overwrites if file exists

Explanation


172. In Program 2-5, what does fileobject.write('\n') do?

A. Writes data without newline
B. Moves file pointer to next line
C. Reads the next line
D. Overwrites previous content

Answer: B. Moves file pointer to next line

Explanation


Mohan Exam

173. What does fileobject.tell() return?

A. Size of the file in bytes
B. Current byte position of file object
C. Number of lines in the file
D. Content of the file

Answer: B. Current byte position of file object

Explanation


174. What is the effect of fileobject.seek(0) in Program 2-5?

A. Moves the file object to end of file
B. Moves the file object to beginning of file
C. Reads the first line
D. Deletes the file

Answer: B. Moves the file object to beginning of file

Explanation


175. Why is a single file object used in Program 2-5?

A. To reduce memory usage
B. To perform simultaneous read and write on the same file
C. To make file read-only
D. To avoid newline characters

Answer: B. To perform simultaneous read and write on the same file

Explanation


176. What happens if we try to read() immediately after opening a file in 'w+' mode without writing any data?

A. Reads garbage values
B. Returns empty string
C. Error occurs
D. Deletes file

Answer: B. Returns empty string

Explanation


177. In Program 2-5, why is fileobject.seek(0) called before reading?

A. To move the file pointer to EOF
B. To move the file pointer to the beginning for reading
C. To write newline
D. To close the file

Answer: B. To move the file pointer to the beginning for reading

Explanation


178. What will happen if fileobject.close() is not called after Program 2-5 execution?

A. File will not save data properly
B. File pointer resets automatically
C. Data will be lost
D. No effect

Answer: A. File will not save data properly

Explanation


179. Which statement reads the entire content after writing in Program 2-5?

A. fileobject.readline()
B. fileobject.readlines()
C. fileobject.read()
D. fileobject.write()

Answer: C. fileobject.read()

Explanation


180. What will fileobject.tell() output if two sentences of 34 and 33 characters were written with \n after each?

A. 69
B. 34
C. 33
D. 0

Answer: A. 69

Explanation


Mohan Exam

181. Why do we use a while True loop in Program 2-5?

A. To repeat file writing until user stops
B. To read EOF
C. To move file pointer
D. To overwrite file

Answer: A. To repeat file writing until user stops

Explanation


182. What is printed after reading the file in Program 2-5?

A. Only last line written
B. Entire file content written till that point
C. Nothing
D. Error

Answer: B. Entire file content written till that point

Explanation


183. Which of the following is TRUE about w+ mode?

A. Appends data without overwriting
B. Deletes existing content before writing
C. Opens file as binary
D. Read-only mode

Answer: B. Deletes existing content before writing

Explanation


184. Which statement is responsible for displaying the byte position of file pointer?

A. fileobject.read()
B. fileobject.seek(0)
C. fileobject.tell()
D. fileobject.write()

Answer: C. fileobject.tell()

Explanation


185. Why is print() used after "WRITING DATA IN THE FILE"?

A. To execute the write method
B. To display a blank line for better formatting
C. To move file pointer
D. To close file

Answer: B. To display a blank line for better formatting

Explanation


186. What is the purpose of the pickle module in Python?

A. To read/write text files
B. To store Python objects as byte streams
C. To execute Python code
D. To create CSV files

Answer: B. To store Python objects as byte streams

Explanation


187. Pickling in Python is analogous to:

A. Reading a text file
B. Writing a Python object to a file as a byte stream
C. Deleting a file
D. Converting a string to integer

Answer: B. Writing a Python object to a file as a byte stream

Explanation


188. What is the opposite of pickling?

A. Serialization
B. Unpickling
C. Encoding
D. Compiling

Answer: B. Unpickling

Explanation


189. Which of the following modes should be used to dump a Python object using pickle?

A. 'r'
B. 'w'
C. 'wb'
D. 'a+'

Answer: C. 'wb'

Explanation


190. What is the correct syntax of the dump() method in the pickle module?


A. dump(fileobject, dataobject)
B. dump(dataobject, fileobject)
C. dump(dataobject)
D. dump(fileobject)

Answer: B. dump(dataobject, fileobject)

Explanation


191. Which method is used to retrieve (load) a Python object from a file?

A. dump()
B. load()
C. read()
D. write()

Answer: B. load()

Explanation


192. Why must we open the file in binary mode for pickle operations?

A. Because pickle writes strings
B. Because pickle works with byte streams
C. Because binary mode speeds up writing
D. Because text files are read-only

Answer: B. Because pickle works with byte streams

Explanation


193. Which of the following can be pickled using the pickle module?

A. Lists and dictionaries
B. Tuples and sets
C. Custom Python objects
D. All of the above

Answer: D. All of the above

Explanation


194. What happens if we try to read a pickled file in text mode ('r')?

A. Object is loaded successfully
B. Garbage characters or error
C. File is overwritten
D. Nothing happens

Answer: B. Garbage characters or error

Explanation


195. Which module is mandatory for pickling in Python?

A. io
B. pickle
C. os
D. csv

Answer: B. pickle

Explanation


196. Which of the following is TRUE about pickling?

A. Pickled files are human-readable
B. Pickled files can only store integers
C. Pickled files are binary representations of objects
D. Pickled files can only be used in text mode

Answer: C. Pickled files are binary representations of objects

Explanation


197. If we want to store the current state of a game in Python, which module is most suitable?

A. csv
B. pickle
C. os
D. json

Answer: B. pickle

Explanation


198. What will happen if we try to dump a file object itself using the pickle module?

A. Works fine
B. Error occurs
C. File is overwritten
D. File is deleted

Answer: B. Error occurs

Explanation


199. Pickle is best suited for which type of data storage?

A. Temporary memory (RAM)
B. Permanent storage as binary objects
C. Plain text CSV files
D. HTML data

Answer: B. Permanent storage as binary objects

Explanation


200. Which statement is correct regarding load() method?

A. It writes an object to a file
B. It reads the byte stream and returns a Python object
C. It deletes the file
D. It converts an object to string

Answer: B. It reads the byte stream and returns a Python object

Explanation


201. What is stored in the file mybinary.dat in Program 2-6?

A. Text data only
B. Python list object as binary data
C. CSV formatted string
D. JSON data

Answer: B. Python list object as binary data

Explanation


202. Which file mode is used for pickling (writing) in Program 2-6?

A. 'r'
B. 'w'
C. 'wb'
D. 'a+'

Answer: C. 'wb'

Explanation


203. What will happen if we try to open mybinary.dat in text mode ('r') and read it?

A. Data is displayed correctly
B. Garbage characters appear
C. Python automatically converts it to text
D. File is overwritten

Answer: B. Garbage characters appear

Explanation


204. Which method is used to retrieve data from mybinary.dat in Program 2-7?

A. read()
B. load()
C. write()
D. writelines()

Answer: B. load()

Explanation


205. After executing Program 2-7, which data type will objectvar have?

A. String
B. List
C. Dictionary
D. Tuple

Answer: B. List

Explanation


206. Why do we need to close the file after pickling/unpickling?

A. To free memory and ensure data is written to disk
B. It is optional
C. Python automatically closes all files
D. To convert the file to text

Answer: A. To free memory and ensure data is written to disk

Explanation


207. In Program 2-8, the same file object is used to append and read records of employees. Which mode is most appropriate?

A. 'w'
B. 'wb'
C. 'ab+'
D. 'r+'

Answer: C. 'ab+'

Explanation


208. Which of the following statements is TRUE about pickle files?

A. Pickle files can be opened in Notepad and edited
B. Pickle files can store Python objects, including lists, tuples, dictionaries
C. Pickle files automatically convert to JSON
D. Pickle files are stored as plain text

Answer: B. Pickle files can store Python objects, including lists, tuples, dictionaries

Explanation


209. What happens if we try to unpickle a file that was never pickled?

A. Returns an empty object
B. Raises a pickle.UnpicklingError
C. Converts file to text
D. Deletes the file

Answer: B. Raises a pickle.UnpicklingError

Explanation


210. Which of the following is required before using dump() or load()?

A. import os
B. import io
C. import pickle
D. No import required

Answer: C. import pickle

Explanation


211. What does the file empfile.dat store in Program 2-8?

A. Text data of employees
B. Pickled Python list objects representing employee records
C. CSV formatted strings
D. JSON objects

Answer: B. Pickled Python list objects representing employee records

Explanation


212. Which mode is used to append data to empfile.dat?

A. 'wb'
B. 'ab'
C. 'r+'
D. 'w+'

Answer: B. 'ab'

Explanation


213. How does the program determine the size of the binary file?

A. os.stat()
B. bfile.tell()
C. len()
D. By reading all records

Answer: B. Using bfile.tell()

Explanation


214. Which method is used to retrieve each employee record from the binary file?

A. read()
B. readline()
C. pickle.load()
D. loadlines()

Answer: C. pickle.load()

Explanation


215. What type of exception is handled when end of file is reached while reading records?

A. FileNotFoundError
B. EOFError
C. ValueError
D. IndexError

Answer: B. EOFError

Explanation


216. What happens if we open empfile.dat in text mode and try to read it?

A. Records are displayed correctly
B. Garbage characters appear
C. Python automatically converts binary to text
D. File is overwritten

Answer: B. Garbage characters appear

Explanation


217. What will happen if pickle.dump() is called with a non-serializable object?

A. Data is stored successfully
B. TypeError is raised
C. File becomes empty
D. Python converts object automatically

Answer: B. TypeError is raised

Explanation


218. Which of the following is TRUE regarding Program 2-8?

A. Multiple employee records can be written without overwriting previous data
B. Each record must overwrite the previous one
C. File is automatically converted to text
D. Records cannot be read after writing

Answer: A. Multiple employee records can be written without overwriting previous data

Explanation


219. What is the type of edata when reading records from the file?

A. String
B. List
C. Dictionary
D. Tuple

Answer: B. List

Explanation


220. Why is with open("empfile.dat","rb") as bfile: used for reading records?

A. Automatically closes the file after block
B. Converts binary to text
C. Reads all records in a single line
D. Appends new records

Answer: A. Automatically closes the file after block

Explanation


221. What type of data is displayed when reading empfile.dat?

A. Strings
B. Lists
C. Dictionaries
D. Tuples

Answer: B. Lists

Explanation


222. What is the purpose of the try..except block in Program 2-8?

A. To catch syntax errors
B. To handle end-of-file (EOF) while reading records
C. To catch division errors
D. To close the file automatically

Answer: B. To handle end-of-file (EOF) while reading records

Explanation


223. Which mode is used to write employee records in Program 2-8?

A. 'wb'
B. 'ab'
C. 'rb'
D. 'r+'

Answer: B. 'ab'

Explanation


224. What does bfile.tell() return in the program?

A. Number of records
B. Current byte position in the file
C. File name
D. Total number of characters in text

Answer: B. Current byte position in the file

Explanation


225. What will happen if empfile.dat is opened in text mode ('r') instead of binary mode ('rb')?

A. Data will be displayed correctly
B. Garbage values will appear
C. Python will automatically convert it to binary
D. File will be erased

Answer: B. Garbage values will appear

Explanation


Mohan Exam

226. Which method is used to store an object in a binary file?

A. write()
B. writelines()
C. pickle.dump()
D. pickle.load()

Answer: C. pickle.dump()

Explanation


227. Which method is used to retrieve an object from a binary file?

A. read()
B. readline()
C. pickle.load()
D. loadlines()

Answer: C. pickle.load()

Explanation


228. What happens if a non-serializable object is passed to pickle.dump()?

A. It is stored as a string
B. Python raises a TypeError
C. File is automatically converted to text
D. Object is ignored

Answer: B. Python raises a TypeError

Explanation


229. What is the total salary of Employee No. 2 in the output?

A. 38250
B. 5300
C. 43550
D. 37000

Answer: C. 43550

Explanation


230. What type of file is empfile.dat?

A. Text file
B. Binary file
C. CSV file
D. JSON file

Answer: B. Binary file

Explanation


231. Which of the following extensions represents a text file in Python?

A. .txt
B. .exe
C. .mp3
D. .jpg

Answer: A. .txt

Explanation


232. Each line of a text file is terminated by a special character called:

A. EOF
B. EOL
C. ESC
D. NULL

Answer: B. EOL

Explanation


233. Binary files are stored as:

A. ASCII characters
B. Human-readable text
C. A stream of bytes
D. Unicode strings

Answer: C. A stream of bytes

Explanation


234. Which Python function is used to open a file?

A. openfile()
B. open()
C. file_open()
D. fopen()

Answer: B. open()

Explanation


235. What happens when close() method is called on a file object?

A. Only the file pointer moves to beginning
B. File content is erased
C. Resources like memory and processor allocated to file are freed
D. Nothing happens

Answer: C. Resources like memory and processor allocated to file are freed

Explanation


236. Which method is used to write a single string to a file?

A. writelines()
B. write()
C. read()
D. readline()

Answer: B. write()

Explanation


237. Which method is used to write multiple strings to a file at once?

A. write()
B. writelines()
C. readlines()
D. append()

Answer: B. writelines()

Explanation


238. The read([n]) method in Python:

A. Reads all lines as a list
B. Reads a complete line including newline
C. Reads a specified number of bytes
D. Writes data to file

Answer: C. Reads a specified number of bytes

Explanation


239. The difference between readline([n]) and readlines() is:

A. readline() reads multiple lines at once, readlines() reads single line
B. readline() reads a single line, readlines() reads all lines as a list
C. Both read all lines but return different formats
D. None of the above

Answer: B. readline() reads a single line, readlines() reads all lines as a list

Explanation


240. What does the tell() method return in Python file handling?

A. File size in bytes
B. Current position of the file pointer in bytes
C. Number of lines in the file
D. Total characters in file

Answer: B. Current position of the file pointer in bytes

Explanation


241. What is the use of seek(offset, reference_point) method?

A. To close the file
B. To move the file pointer to a specific position
C. To read all lines
D. To delete file contents

Answer: B. To move the file pointer to a specific position

Explanation


Computer Science Class XII NCERT book [Download]

Class 12 Computer Science MCQs [link]


Keep Learning, Keep Growing with Mohan Exam

At Mohan Exam, we believe that learning is not just about memorizing answers — it’s about understanding concepts deeply and applying them with confidence.

Your journey toward success starts with curiosity, consistency, and the right guidance — and we’re here to provide all three.
Because when you learn better, you grow stronger, and achieve greater heights.

So, keep exploring… keep practicing… and keep shining with Mohan Exam — where learning meets excellence!

Leave a Comment