100% found this document useful (1 vote)
4K views3 pages

CBSE Class XII CS File Handling Questions

Bbbb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
4K views3 pages

CBSE Class XII CS File Handling Questions

Bbbb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Important CBSE Class XII Computer

Science Questions on File Handling


Short Answer Questions (2-3 marks)
 Q: Explain the purpose of file handling in Python.

A: File handling allows storing data permanently on a disk. It enables reading from and
writing to files, making data persistence possible.

 Q: Differentiate between text files and binary files.

A: Text files store data in human-readable ASCII or Unicode format, while binary files store
data in binary (0s and 1s) form, which is directly readable by machines.

 Q: Write a Python code snippet to open a file in write mode, write 'Hello, World!' to it,
and close it.

A: with open("example.txt", "w") as file:


file.write("Hello, World!")

 Q: Explain the difference between write() and writelines() functions in Python.

A: write() writes a single string to the file, while writelines() writes a list of strings to the file
in sequence.

 Q: What does the seek() method do in file handling?

A: The seek() method moves the file pointer to a specific location within the file.

Long Answer Questions (4-5 marks)


 Q: Write a Python program to count the number of words in a text file.

A: def count_words(filename):
with open(filename, "r") as file:
data = file.read()
words = data.split()
return len(words)

filename = "sample.txt"
print("Total words:", count_words(filename))

 Q: Explain file handling modes in Python with examples of each mode.

A: - 'r': Read mode - Opens a file for reading.


- 'w': Write mode - Opens a file for writing, creates it if it doesn't exist, and truncates if it
exists.
- 'a': Append mode - Opens a file for appending data at the end without truncating.
- 'rb', 'wb', 'ab': Binary modes for reading, writing, and appending.

 Q: Write a program to copy the contents of one text file to another.

A: with open("source.txt", "r") as source:


with open("destination.txt", "w") as destination:
destination.write(source.read())

 Q: Describe the process of reading and writing a binary file with an example.

A: # Writing to binary file


with open("data.dat", "wb") as file:
file.write(b"Hello Binary!")

# Reading from binary file


with open("data.dat", "rb") as file:
data = file.read()
print(data)

Previous Year Questions (4-5 marks)


 Q: Write a Python program to read student records from a text file and display students
who scored more than 75%.

A: def display_high_scorers(filename):
with open(filename, "r") as file:
for line in file:
name, marks = line.strip().split(',')
if int(marks) > 75:
print(name, marks)

filename = "students.txt" # Each line in format "Name, Marks"


display_high_scorers(filename)

 Q: Explain and demonstrate exception handling while performing file operations.

A: try:
with open("example.txt", "r") as file:
print(file.read())
except FileNotFoundError:
print("File not found. Please check the file path.")

 Q: Write a Python program to append data to a file only if it exists; otherwise, create a
new file and write data.

A: try:
with open("data.txt", "a") as file:
file.write("Appended text.\n")
except FileNotFoundError:
with open("data.txt", "w") as file:
file.write("New file created and text added.\n")

You might also like