Class 12 Computer Science - File Handling (40 Marks)
General Instructions:
- All questions are compulsory.
- Python is to be used for all programming questions.
- Answer the questions in the given sequence.
Section A: Objective Questions (1 × 5 = 5 marks)
Q1. Answer the following questions:
(a) What is the purpose of the close() function in file handling?
(b) Name any two file modes used in Python.
(c) What will [Link](5) do if f is a file object?
(d) Which module is used for binary file handling using object serialization?
(e) What is the difference between [Link]() and [Link]()?
Section B: Short Answer Questions (3 × 5 = 15 marks)
Q2. Write a Python program to write the following three lines to a text file named [Link]:
Python is powerful.
File handling is easy.
Let's practice more.
Q3. Write a function that reads a file [Link] and counts how many times the word "computer"
appears in it (case insensitive).
Q4. Differentiate between text file and binary file with two points and examples each.
Q5. Write a Python program to append a line "Keep learning Python!" to an existing file [Link].
Q6. Write a Python function to display the contents of a binary file [Link], which contains
student records (roll number, name) stored using the pickle module.
Section C: Long Answer / Programmatic Questions (5 × 4 = 20 marks)
Q7. Write a Python program to copy contents from a file [Link] to another file [Link].
Q8. Write a Python program to create a binary file [Link] that stores the following information for
3 students using pickle:
- Roll number (int)
- Name (string)
- Marks (float)
Q9. Write a program to count the number of uppercase letters in a file [Link].
Q10. A file [Link] contains numbers separated by space. Write a program to read the file and
print the maximum number.
Solutions
Section A Answers:
Q1.
(a) It closes the file and frees up system resources.
(b) 'r', 'w', 'a', 'rb', 'wb'
(c) Reads the first 5 characters from the file.
(d) pickle
(e) [Link]() reads the entire file; [Link]() reads only one line at a time.
Section B and C: Selected Solutions
Q2.
with open("[Link]", "w") as f:
[Link]("Python is powerful.\n")
[Link]("File handling is easy.\n")
[Link]("Let's practice more.\n")
Q3.
def count_word():
count = 0
with open("[Link]", "r") as f:
for line in f:
words = [Link]().split()
count += [Link]("computer")
print("Occurrences of 'computer':", count)
Q4.
Text File vs Binary File:
- Human-readable vs Machine-readable
- Uses read/write vs [Link]/dump
Q5.
with open("[Link]", "a") as f:
[Link]("\nKeep learning Python!")
Q6.
import pickle
def display_students():
try:
with open("[Link]", "rb") as f:
while True:
student = [Link](f)
print(student)
except EOFError:
pass
Q7.
with open("[Link]", "r") as src, open("[Link]", "w") as dest:
for line in src:
[Link](line)
Q8.
import pickle
with open("[Link]", "wb") as f:
for i in range(3):
roll = int(input("Enter roll: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
[Link]([roll, name, marks], f)
Q9.
def count_uppercase():
count = 0
with open("[Link]", "r") as f:
for line in f:
for ch in line:
if [Link]():
count += 1
print("Uppercase letters:", count)
Q10.
def find_max_number():
with open("[Link]", "r") as f:
numbers = list(map(int, [Link]().split()))
print("Maximum number:", max(numbers))