0% found this document useful (0 votes)
5 views4 pages

CS Text File Handling Solutions

The document contains holiday homework solutions for Class 12 Computer Science focusing on text file handling. It includes answers to 1-mark, 2-mark, 3-mark, and 4-mark questions, as well as competency-based programming tasks related to reading, writing, and analyzing files. The solutions cover various file operations, string manipulations, and data logging techniques.

Uploaded by

SUNNY HEMBRAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

CS Text File Handling Solutions

The document contains holiday homework solutions for Class 12 Computer Science focusing on text file handling. It includes answers to 1-mark, 2-mark, 3-mark, and 4-mark questions, as well as competency-based programming tasks related to reading, writing, and analyzing files. The solutions cover various file operations, string manipulations, and data logging techniques.

Uploaded by

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

Class 12 Computer Science - Text File Handling - Holiday Homework Solutions

===========================
1-MARK QUESTIONS - Answers
===========================

1. (a) f.read(3)
2. (b) text file
3. (b) writelines()
4. (a) True
5. (c) No output
6. (b) Education, Hub, Learning is
7. (d) 8
8. (a) 44
9. (b) 8
10. (a) 2

===========================
2-MARK QUESTIONS - Answers
===========================

Q1.
with open("file1.txt", "r") as f:
for line in f:
print(line.strip())

Q2.
Output: 'hello,world!\nhow are you'

Q3.
After execution: File content = 'bye'

Q4.
(i) Text file in append mode
(ii) f1.write("abc")

Q5.
Modes that preserve data: rb, a+b, r+, ab

Q6.
f.read() -> str
f.read(10) -> str
f.readline() -> str
f.readlines() -> list of str

Q7.
Output will be blank due to EOF after first read.

Q8.
stdin - input stream (keyboard)
stdout - output stream (screen)
stderr - error stream

Q9.
flush() forces buffer content to file before close.

============================
3-MARK QUESTIONS - Answers
============================

Q1.
def file_stats(filename):
with open(filename, 'r') as f:
content = f.read()
lines = content.splitlines()
words = content.split()
print("Size in bytes:", len(content))
print("Number of lines:", len(lines))
print("Number of words:", len(words))

Q2.
with open("data.txt", "r") as f:
lines = f.readlines()
if lines:
print(lines[-1].strip())

Q3.
count_to = 0
count_the = 0
with open("python.txt", "r") as f:
for line in f:
words = line.lower().split()
count_to += words.count("to")
count_the += words.count("the")
print("'to':", count_to, ", 'the':", count_the)

Q4.
with open("python.txt", "r") as f:
for i, line in enumerate(f, start=1):
print(f"{i}: {line.strip()}")

Q5.
with open("python.txt", "r") as f:
for line in f:
if "to" in line.lower().split():
print(line.strip())

============================
4-MARK QUESTIONS - Answers
============================

Q1.
with open("city.txt", "w+") as f:
f.write("Delhi\nMumbai\nChennai\nKolkata")
f.seek(0)
print(f.read())

Q2.
Output:
Friends are honest
, Friends
are best !

Q3.
def count_lines():
with open("friends.txt", "r") as f:
count = len(f.readlines())
print("Total lines:", count)

Q4.
def display_oddLines():
with open("friends.txt", "r") as f:
lines = f.readlines()
for i in range(0, len(lines), 2):
print(lines[i].strip())

Q5.
def cust_data():
with open("customer.txt", "a") as f:
name = input("Enter your name: ")
age = input("Enter your age: ")
f.write(f"{name}, {age}\n")

=========================================
Competency-Based Programming Questions
=========================================

Reading/Writing Files

1.
with open("data.txt", "r") as f:
lines = f.readlines()
print("Total number of lines:", len(lines))

2.
with open("source.txt", "r") as src, open("destination.txt", "w") as dest:
for line in src:
dest.write(line)

3.
with open("marks.txt", "r") as f:
for line in f:
name, mark = line.split()
if int(mark) > 75:
print(name)

String and Line Manipulation

4.
count = 0
with open("notes.txt", "r") as f:
for line in f:
count += line.count("Python")
print("Occurrences of 'Python':", count)

5.
with open("data.txt", "r") as f:
for line in f:
if line and line[0].isupper():
print(line.strip())

6.
vowels = "aeiouAEIOU"
with open("poem.txt", "r") as f:
for line in f:
count = sum(1 for char in line if char in vowels)
print(f"Vowels in line: {count}")

Writing/Appending Files

7.
from datetime import datetime
def log_message():
msg = input("Enter message: ")
with open("log.txt", "a") as f:
f.write(f"{datetime.now()}: {msg}\n")

8.
with open("feedback.txt", "w") as f:
for i in range(5):
feedback = input(f"Feedback {i+1}: ")
f.write(feedback + "\n")

File Analysis

9.
with open("story.txt", "r") as f:
words = f.read().split()
longest = max(words, key=len)
print("Longest word:", longest)

10.
with open("data.txt", "r") as fin, open("cleaned_data.txt", "w") as fout:
for line in fin:
if line.strip():
fout.write(line)

You might also like