Skip to content
TestMacher
Chapter 3 · Class 12 Computer Science

File Handling — Important Questions

32 questions With answers CBSE format

SUMMARY: The chapter on File Handling in Class 12 Computer Science focuses on the concepts and techniques for managing files in Python, including reading from and writing to files.
KEY TOPICS: file operations, text files, binary files, file handling functions, reading files, writing files, file modes, file pointers, exception handling in file operations, CSV files

Q1 1 Mark

Which mode opens a file for reading only?

Ar
Bw
Ca
Drb
Check answerHide answer
Correct answer: Option 1 — r
Q2 1 Mark

Which method is used to read all lines of a file as a list?

Aread()
Breadline()
Creadlines()
Dgetlines()
Check answerHide answer
Correct answer: Option 3 — readlines()
Q3 1 Mark

Which file mode opens a file for writing and truncates the existing content?

Ar
Bw
Ca
Dr+
Check answerHide answer
Correct answer: Option 2 — w
Q4 1 Mark

Which of the following is used to write data into a binary file in Python?

Awrite()
Bpickle.dump()
Csave()
Dstore()
Check answerHide answer
Correct answer: Option 2 — pickle.dump()
Q5 1 Mark

The CSV file format stores data as:

AComma-separated values
BCell-stored values
CCompressed values
DCoded values
Check answerHide answer
Correct answer: Option 1 — Comma-separated values
Q6 3 Marks

Differentiate between text file and binary file with two points each.

Q7 3 Marks

Explain the difference between read() readline() and readlines() methods.

Q8 3 Marks

What is the use of the with statement in file handling? Give one example.

Q9 3 Marks

Differentiate between pickle.dump() and pickle.load() methods.

Q10 3 Marks

Why is it important to close a file after operations are done?

Q11 6 Marks

Write a Python program to count the number of lines words and characters in a text file.

Q12 6 Marks

Discuss different file modes in Python with their meaning and use cases.

Q13 6 Marks

Write a Python program that reads a CSV file containing student records (name marks) and writes only the passed students (marks >= 33) to a new CSV file.

Q14 6 Marks

Explain pickling and unpickling in Python with one example using a list of tuples.

Q15 6 Marks

Write a Python program that uses pickle to write and read a list of student records (id name marks) to/from a binary file.

Q16 6 Marks

Differentiate between text file and binary file in tabular form on five features.

Q17 1 Mark

Assertion (A): Files should be closed after use.

Reason (R): Open files consume system resources and may corrupt data if not closed.

Show explanationHide explanation
Correct answer: Option 1 — Both A and R are true, and R is the correct explanation of A.
Q18 1 Mark

Assertion (A): The with statement automatically closes the file.

Reason (R): It uses a context manager to handle cleanup.

Show explanationHide explanation
Correct answer: Option 1 — Both A and R are true, and R is the correct explanation of A.
Q19 1 Mark

Assertion (A): Binary files store data in raw bytes.

Reason (R): They are not human-readable in a text editor.

Show explanationHide explanation
Correct answer: Option 1 — Both A and R are true, and R is the correct explanation of A.
Q20 1 Mark

Assertion (A): The pickle module is used for serialisation.

Reason (R): It converts Python objects to byte streams for storage.

Show explanationHide explanation
Correct answer: Option 1 — Both A and R are true, and R is the correct explanation of A.
Q21 1 Mark

Assertion (A): CSV files can be processed using the csv module.

Reason (R): It provides reader and writer objects for easy CSV manipulation.

Show explanationHide explanation
Correct answer: Option 1 — Both A and R are true, and R is the correct explanation of A.
Q22 1 Mark

Statement 1: A text file stores data as characters.

Statement 2: Binary files store data as raw bytes.

Show answerHide answer
Correct answer: Option 1 — Both statements are true.
Q23 1 Mark

Statement 1: The open() function returns a file object.

Statement 2: The file object provides methods to read and write data.

Show answerHide answer
Correct answer: Option 1 — Both statements are true.
Q24 1 Mark

Statement 1: The 'a' mode opens a file for appending.

Statement 2: New data is added at the end without affecting existing content.

Show answerHide answer
Correct answer: Option 1 — Both statements are true.
Q25 1 Mark

Statement 1: The seek() method moves the file pointer to a specified position.

Statement 2: The tell() method returns the current position of the file pointer.

Show answerHide answer
Correct answer: Option 1 — Both statements are true.
Q26 1 Mark

Statement 1: Pickling is the process of converting Python objects to byte streams.

Statement 2: Unpickling reverses the process.

Show answerHide answer
Correct answer: Option 1 — Both statements are true.
Q27 3 Marks
A teacher has a CSV file student_marks.csv containing rows like name maths physics chemistry. She wants to write a Python program to read the file count the number of students compute the average marks per subject and identify students who passed all subjects (>= 33).
  1. Which file method returns all lines as a list?
    Aread()
    Breadlines()
    Creadline()
    Dreadall()
  2. Which Python statement automatically closes the file after use?
    Awith
    Bfrom
    Cif
    Dfor
  3. Write the complete program to read the CSV count students compute averages and list those passing all subjects.
Show answersHide answers
1. Option 2 — readlines()
2. Option 1 — with
3. import csv; with open('student_marks.csv') as f: reader = csv.reader(f); next(reader); rows = list(reader). count = len(rows). avg_m = sum(int(r[1]) for r in rows) / count. passed = [r[0] for r in rows if all(int(x) >= 33 for x in r[1:])]. The csv module simplifies CSV handling; with statement ensures the file is closed even on errors.
Q28 6 Marks

Identify the file mode and what each does.

ModeOperation
r?
w?
a?
r+?
rb?
wb?
Q29 5 Marks

Predict file pointer position after each operation.

OperationPosition after
open and tell()?
read(5)?
seek(10)?
read(3) after seek(10)?
seek(0)?
seek(0, 2)?
Q30 5 Marks

Match each pandas / file concept with its description (mixed concept review).

ConceptDescription
open()?
read()?
readlines()?
with statement?
seek()?
Q31 3 Marks

Study the table of file modes and answer:

ModeOperationFile must exist?
rReadYes
wWrite (truncates)No
aAppendNo
r+Read + WriteYes
rbRead binaryYes
wbWrite binaryNo
  1. Which mode opens a file for reading only and requires the file to exist?
    Ar
    Bw
    Ca
    Dr+
  2. Which mode appends data to the end of a file?
    Ar
    Bw
    Ca
    Dr+
  3. Discuss the common file modes in Python with use cases.
Show answersHide answers
1. Option 1 — r
2. Option 3 — a
3. Python file modes specify the operation (read write append) and whether the file is text or binary. r requires the file to exist; w creates or truncates; a appends. Adding b makes it binary (e.g., rb wb ab). Adding + adds the other operation (e.g., r+ for read and write). The default is text mode.
Q32 3 Marks

Study the file modes diagram and answer:

File Handling figure
  1. Which mode opens a file for writing and truncates existing content?
    Ar
    Bw
    Ca
    Dr+
  2. Which mode appends data without truncating?
    Ar
    Bw
    Ca
    Drb
  3. Discuss common file modes in Python with use cases.
Show answersHide answers
1. Option 2 — w
2. Option 3 — a
3. File modes specify the operation. r requires file to exist. w creates or truncates. a appends. Adding b makes it binary (rb wb ab). Adding + adds the other operation (r+ for read+write). The with statement automatically closes the file even on errors.

Make a full Computer Science paper on File Handling.

Pick the question mix, set the marks, hit generate. You get a ready-to-print paper with an answer key.

Generate your paper — free