Text Files: Class 12 PYQs 2024–2026 with solutions
All 6 CBSE board questions asked from Text Files in 3 board papers, with the year, set and marks of each. 5 are solved free below. The Class 12 Computer Science PYQ Book: Solved Board Papers 2024-2026 (CBSE) has every solution, all chapters and 3 full board papers.
Text Files: board weightage
Board weightage 2024–2026
| Year | 2024 | 2025 | 2026 |
|---|---|---|---|
| Questions | 4 | 1 | 1 |
| Marks | 8 | 3 | 3 |
Most asked topics
6 questions from 3 papers. Repeated questions are shown once with every year they were asked.
1 mark: MCQ and Assertion–Reason
F=open('CONTENT.TXT'). Which is an invalid statement? (a) F.seek(1,0) (b) F.seek(0,1) (c) F.seek(0,-1) (d) F.seek(0,2)
Solution
(c) F.seek(0,-1). The reference point can only be 0, 1 or 2; −1 is invalid (ValueError: invalid whence).
2 marks: very short answer
(A)(i) Differentiate between 'w' and 'a' file modes.
Solution
'w': opens for writing; creates the file if absent; the pointer is at the beginning, so existing contents are erased. 'a': opens for appending; creates the file if absent; the pointer is at the end, so new data is added after the existing contents.
(B)(i) What is the advantage of using the with clause while opening a data file? Give its syntax.
Solution
A file opened with the with clause is closed automatically once control comes out of the with block (even after an exception). Syntax: with open(file_name, access_mode) as file_object: e.g. with open("myfile.txt","r+") as fo: content = fo.read().
3 marks: short answer
(a) Write a function that counts and returns the number of digits in "Space.txt" (sample returns 11). OR (b) Display the words in which lowercase 'e' appears at least twice in 'Space.txt'.
Solution
def CountDigits():Digits in 1969, 400, 2030 → 4 + 3 + 4 = 11.
with open("Space.txt", "r") as F:
Data = F.read()
C = 0
for CH in Data:
if CH.isdigit():
C += 1
return C
def showWords():
with open("Space.txt", "r") as F:
for word in F.read().split():
if word.count('e') >= 2:
print(word, end=' ')
incredible advancements science. agencies serves research.
(a) Write a function that displays all lines containing the word 'vote' from "Elections.txt". OR (b) Display all words starting and ending with a vowel from "Report.txt", separated by a space.
Solution
def PrintVote():
with open("Elections.txt") as F:
for Line in F:
if "vote" in Line.split(): # whole word only
print(Line, end="")
For the sample file it prints lines 1 and 3; "votes" and "voting" in lines 2 and 4 are not the word "vote", which is why split() is used instead of "vote" in Line.
def vowels():
with open("Report.txt") as F:
for W in F.read().split():
if W[0] in "aeiouAEIOU" and W[-1] in "aeiouAEIOU":
print(W, end=" ")
Once a a awesome able
(A) Write showInLines() that reads STORY.TXT and displays every sentence in a separate line (a sentence ends with . ? or !). OR (B) Write c_words() that separately counts and displays the number of uppercase and lowercase letters in Words.txt.
Get every solution for all chapters
- Every board question of every chapter, each fully solved the way the board gives marks
- 3 complete board papers, each followed by its solutions
- Weightage and most-asked topics for every chapter
- Instant PDF download, colour and black & white print copies
Revising the chapter first? Complete Computer Science Notes for Class 12 (CBSE 083, 2026-27) →
What you get in the pack
- CBSE Class 12 Computer Science board papers 2024 to 2026 (3 full board papers)
- All 14 chapters and 3 board papers complete
- Part A: all 14 chapters, questions sorted chapter-wise and by marks
- Year-wise weightage table and most-asked topics for every chapter
- Part B: each full board paper followed by its complete solutions
- Full programs; every output traced and verified by running the code
- Only the current syllabus in Part A
All chapters in this pack
- PYQ Ch 1 Python Revision Tour
- PYQ Ch 2 Functions
- PYQ Ch 3 Exception Handling
- PYQ Ch 4 Text Files
- PYQ Ch 5 Binary Files
- PYQ Ch 6 CSV Files
- PYQ Ch 7 Stack
- PYQ Ch 8 Computer Networks I: Evolution and Data Communication
- PYQ Ch 9 Computer Networks II: Devices, Topologies and Types
- PYQ Ch 10 Network Protocols and Web Services
- PYQ Ch 11 Database Concepts
- PYQ Ch 12 SQL: DDL, DML and Queries
- PYQ Ch 13 SQL: Aggregates, Grouping and Joins
- PYQ Ch 14 Interface of Python with MySQL
- Board Paper 2026 Set 91 with Solutions
- Board Paper 2025 Set 91 with Solutions
- Board Paper 2024 Set 91 with Solutions
