Q1. Which of the following statements is used to open a file named "data.txt" in append and read mode?
(a) f = open("data.txt", "r+")
(b) f = open("data.txt", "a+")
(c) f = open("data.txt", "w")
(d) f = open("data.txt", "rb")
Q2. The tell() method in Python file handling returns:
(a) The total number of characters in the file
(b) The current position of the file object in bytes from the beginning of the file
(c) The end-of-file position
(d) The number of lines in the file
Q3. Which method is used to write a sequence of strings to a text file?
(a) write()
(b) writelines()
(c) readlines()
(d) dump()
Q4. Assertion (A): Binary files store data as a stream of bytes that do not represent ASCII values of characters.
Reason (R): Binary files can be directly opened and read using any text editor without showing garbage values.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
Q5. The default End of Line (EOL) character used by Python while writing to a text file is:
(a) \t
(b) ,
(c) \n
(d) ;
Q6. Which of the following file modes positions the file object at the end of the file when opened?
(a) r
(b) w
(c) a
(d) rb
Q7. Assertion (A): The with clause automatically closes the file once the control moves out of the block.
Reason (R): Using the with clause eliminates the need to explicitly call the close() method.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
Q8. The pickle.dump() method is used to:
(a) Read data from a text file line by line
(b) Convert a Python object into a byte stream and write it to a binary file
(c) Write multiple strings to a text file
(d) Return the current position of the file object
Q9. What will be the output of fileobject.read(5) if the file contains the string "Hello World"?
(a) "Hello World"
(b) "Hello"
(c) "Hel"
(d) An error
Q10. Which module must be imported to perform pickling and unpickling operations?
(a) io
(b) pickle
(c) os
(d) sys
Q1. Differentiate between a text file and a binary file (any two points).
Q2. What is the difference between read() and readline() methods?
Q3. Write the output of the following code fragment:
python
f = open("test.txt", "w")
f.write("CBSE\nClass 12")
f.close()
f = open("test.txt", "r")
print(f.read(4))
f.close()
Q4. What is the purpose of the seek() method? Give its syntax.
Q5. Differentiate between write() and writelines() methods.
Q6. What does the file_object.tell() method return? Explain with reference to the chapter.
Q1. Write a short code fragment that opens a file "report.txt" in write mode, writes two lines using writelines(), and then closes the file. Also state where the file object will be positioned after opening in write mode.
Q2. Identify the error in the following code and write the corrected version:
python
f = open("data.txt")
print(f.read())
f.write("New line")
f.close()
Q3. What will be the output of the following code? Explain briefly.
python
f = open("myfile.txt", "w")
lines = ["Line1\n", "Line2\n"]
f.writelines(lines)
f.close()
f = open("myfile.txt", "r")
print(f.readlines())
f.close()
Q4. Explain the advantage of using the with clause to open a file. Write one example statement using with.
Q5. Write a code fragment that uses readline() inside a loop to read and display all lines from a text file named "notes.txt".
Q1. Write a complete Python program that accepts multiple sentences from the user until the user enters "END". Write all sentences to a text file "story.txt" (one sentence per line). Then reopen the file in read mode and display only those sentences that start with an uppercase letter.
Q2. Explain the working of seek() and tell() methods with a dry-run using a trace table. Assume a file "data.txt" contains the text "PythonFileHandling". Show the position of the file object after each operation in the following code:
python
f = open("data.txt", "r+")
print(f.tell())
f.seek(6)
print(f.tell())
print(f.read(4))
print(f.tell())
f.close()
Q3. Write a complete Python program using the pickle module to store a list containing employee details (empno, name, salary) into a binary file "emp.dat". The program should allow the user to enter multiple records and then read and display all records from the file.
Q1. A school wants to maintain a permanent record of students' names and marks in a text file "student.txt". The file is created once and later used for reading data.
(a) Which file mode should be used to create the file initially?
(b) Which method will be suitable to write multiple student records at once?
(c) After writing, which method can be used to read the entire content as a list of lines?
(d) If the file object needs to be moved to the 10th byte from the beginning before reading, which method will be used?
Q2. A programmer wants to store a Python list object permanently in a file so that it can be retrieved later in the same form.
(a) Which module should be imported for this purpose?
(b) Which method will be used to write the list object into a binary file?
(c) In which mode must the binary file be opened for writing the object?
(d) Which method will be used to retrieve the stored object from the binary file?
Q1. (b)
Q2. (b)
Q3. (b)
Q4. (c) [A is true; R is false because binary files show garbage values in text editors]
Q5. (c)
Q6. (c)
Q7. (a)
Q8. (b)
Q9. (b)
Q10. (b)
Q1. Text file stores human-readable characters (ASCII); binary file stores bytes representing non-text data (1 mark). Text files can be opened in any text editor; binary files require specific software (1 mark).
Q2. read(n) reads specified bytes (or whole file); readline(n) reads one line up to newline or specified bytes (2 marks).
Q3. Output: CBSE (1 mark). Because read(4) reads first 4 characters from the file (1 mark).
Q4. seek() positions the file object at a specified byte offset. Syntax: file_object.seek(offset [, reference_point]) (2 marks).
Q5. write() writes a single string and returns number of characters written; writelines() writes an iterable of strings and does not return count (2 marks).
Q6. It returns the current byte position of the file object from the beginning of the file (2 marks).
Q1. Correct code:
python
f = open("report.txt", "w")
f.writelines(["First line\n", "Second line\n"])
f.close()
File object is positioned at the beginning (3 marks).
Q2. Error: File opened in read mode cannot be written to.
Corrected:
python
f = open("data.txt", "w+")
print(f.read())
f.write("New line")
f.close()
(3 marks)
Q3. Output:
python
['Line1\n', 'Line2\n']
(2 marks). writelines() writes the list and readlines() returns content as list of strings ending with \n (1 mark).
Q4. Advantage: File is closed automatically when control exits the block, even if exception occurs (2 marks). Example: with open("file.txt", "r") as f: data = f.read() (1 mark).
Q5. Correct code fragment:
python
f = open("notes.txt", "r")
line = f.readline()
while line:
print(line)
line = f.readline()
f.close()
(3 marks)
Q1. Complete program:
```python
f = open("story.txt", "w")
while True:
sent = input("Enter sentence (END to stop): ")
if sent == "END":
break
f.write(sent + "\n")
f.close()
f = open("story.txt", "r") for line in f: if line[0].isupper(): print(line, end="") f.close() ``` (5 marks – 2 for writing loop, 2 for reading & checking, 1 for correct file handling)
Q2. Trace table (positions after each statement):
- After print(f.tell()) → 0
- After f.seek(6) → 6
- After print(f.read(4)) → 10
- After print(f.tell()) → 10
(5 marks – 2 for explanation of seek/tell, 3 for correct trace)
Q3. Complete program using pickle (similar to NCERT Program 2-8 structure) with dump() and load() (5 marks).
Q1. (a) w or w+ (1 mark)
(b) writelines() (1 mark)
(c) readlines() (1 mark)
(d) seek(10, 0) (1 mark)
Q2. (a) pickle (1 mark)
(b) dump() (1 mark)
(c) wb (1 mark)
(d) load() (1 mark)
All questions are answerable from the NCERT chapter text. Reviewed by GFIS faculty.