Computer Science

Python Text Files MCQs | 25 Practice Questions with Answers | CBSE Class 12 Computer Science

Class 12 · Computer Science

Python Text Files MCQs (10 Questions)

Practice these multiple-choice questions to strengthen your understanding of Python Text Files. Click Show Answer to reveal the correct answer and explanation.


Q1. Which function is used to open a text file in Python?

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

Show Answer

Answer: C. open()

Explanation: The open() function is used to open an existing file or create a new file.


Q2. Which mode is used to open a file for reading only?

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

Show Answer

Answer: C. r

Explanation: The r mode opens an existing file for reading only.


Q3. Which mode creates a new file or overwrites an existing file?

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

Show Answer

Answer: B. w

Explanation: The w mode opens a file for writing and removes all existing contents if the file already exists.


Q4. Which mode is used to append data at the end of a file?

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

Show Answer

Answer: C. a

Explanation: The a mode appends new data to the end of the file without deleting existing contents.


Q5. Which file mode allows both reading and writing without deleting existing contents?

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

Show Answer

Answer: A. r+

Explanation: The r+ mode allows both reading and writing while preserving existing data.


Q6. Which file mode allows reading and writing but removes all existing contents?

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

Show Answer

Answer: B. w+

Explanation: The w+ mode opens a file for both reading and writing but truncates the existing file.


Q7. Which file mode allows reading as well as appending data?

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

Show Answer

Answer: B. a+

Explanation: The a+ mode allows reading and appending data to a file.


Q8. Which method is used to close an opened file?

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

Show Answer

Answer: C. close()

Explanation: The close() method closes the file and releases associated resources.


Q9. Which statement automatically closes the file after its block is executed?

A. using
B. open
C. with
D. file

Show Answer

Answer: C. with

Explanation: The with statement automatically closes the file when the block finishes execution.


Q10. What is the advantage of using the with statement while working with files?

A. It makes the file read-only.
B. It automatically closes the file after use.
C. It increases file size.
D. It permanently stores data in memory.

Show Answer

Answer: B. It automatically closes the file after use.

Explanation: The with statement ensures proper resource management by automatically closing the file even if an exception occurs.


Q11. Which method is used to write a string to a text file?

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

Show Answer

Answer: B. write()

Explanation: The write() method writes a single string to a text file.


Q12. Which method is used to write multiple strings to a text file?

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

Show Answer

Answer: C. writelines()

Explanation: The writelines() method writes multiple strings from a list or other iterable to a file.


Q13. Which method reads the entire contents of a text file?

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

Show Answer

Answer: C. read()

Explanation: The read() method reads the complete contents of a file.


Q14. Which method reads only one line from a text file?

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

Show Answer

Answer: B. readline()

Explanation: The readline() method reads one line at a time from a text file.


Q15. Which method returns all lines of a text file as a list?

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

Show Answer

Answer: C. readlines()

Explanation: The readlines() method returns all lines of a file as a list of strings.


Q16. What will be the output of the following code if the file contains the text Python?

f=open("demo.txt","r")
print(f.read())
f.close()

A. demo.txt
B. Python
C. Error
D. None

Show Answer

Answer: B. Python

Explanation: The read() method reads and returns the complete contents of the file.


Q17. Which method returns the current position of the file pointer?

A. seek()
B. tell()
C. read()
D. pointer()

Show Answer

Answer: B. tell()

Explanation: The tell() method returns the current location of the file pointer in bytes.


Q18. Which method is used to move the file pointer to a specified position?

A. tell()
B. move()
C. seek()
D. position()

Show Answer

Answer: C. seek()

Explanation: The seek() method moves the file pointer to the specified byte position.


Q19. What will be the output of the following code if the file contains Computer?

f=open("demo.txt","r")
print(f.read(4))
f.close()

A. Computer
B. Comp
C. Com
D. Error

Show Answer

Answer: B. Comp

Explanation: The read(4) method reads only the first four characters from the file.


Q20. Which statement is true about the write() method?

A. It always appends data to the file.
B. It returns the number of characters written to the file.
C. It reads data from the file.
D. It closes the file automatically.

Show Answer

Answer: B. It returns the number of characters written to the file.

Explanation: The write() method writes the specified string to the file and returns the number of characters written.


Q21. What will be the output of the following code if the file contains Python Programming?

f=open("demo.txt","r")
print(f.readline())
f.close()

A. Python Programming
B. Only Python
C. Error
D. None

Show Answer

Answer: A. Python Programming

Explanation: Since the file contains only one line, readline() returns the complete line.


Q22. Which method is commonly used to modify the contents of a text file?

A. read()
B. write()
C. Open the file in an appropriate mode, read the contents, make the required changes, and write the updated data back to the file.
D. close()

Show Answer

Answer: C.

Explanation: Since text files do not support direct modification, the usual approach is to read the existing data, modify it in the program, and write the updated contents back to the file.


Q23. What will be the output of the following code?

f=open("demo.txt","w")
print(f.write("Python"))
f.close()

A. Python
B. 6
C. Error
D. None

Show Answer

Answer: B. 6

Explanation: The write() method returns the number of characters written to the file. The word Python contains six characters.


Q24. What will be the output of the following code?

f=open("demo.txt","w")
f.writelines(["CBSE\n","Python\n"])
f.close()

A. Error
B. The file will contain two separate lines: CBSE and Python.
C. Nothing will be written to the file.
D. Only the first line will be written.

Show Answer

Answer: B.

Explanation: The writelines() method writes all strings from the list to the file. Newline characters (\n) create separate lines.


Q25. Which statement about Python text file handling is correct?

A. A file must always be closed after use.
B. The with statement automatically closes the file.
C. File handling supports reading, writing, and appending data.
D. All of these

Show Answer

Answer: D. All of these

Explanation: Python provides multiple file handling operations. Files should always be closed after use, or the with statement should be used to close them automatically.


Answer Key

Q.No. Answer Q.No. Answer Q.No. Answer
1C 10B 19B
2C 11B 20B
3B 12C 21A
4C 13C 22C
5A 14B 23B
6B 15C 24B
7B 16B 25D
8C 17B
9C 18C

Practice Tips

  • Remember the purpose of each file mode: r, r+, w, w+, a, and a+.
  • Understand the difference between read(), readline(), and readlines().
  • Revise the difference between write() and writelines().
  • Practice using seek() and tell() to control the file pointer.
  • Remember that text files do not support direct insertion or deletion of data. To modify data, read the contents, make the required changes, and write the updated contents back to the file.
  • Prefer using the with statement because it automatically closes the file after use.
  • Practice output-based questions involving different file modes and file handling methods, as they are frequently asked in CBSE board examinations.