Computer Science Class 12: Python programs that repeat in exams
Class 12 Computer Science board exams test more than just syntax; they evaluate your logic in handling data structures, file operations, and modular programming. Students often lose marks in coding sections not due to a lack of understanding, but due to repetitive errors in string manipulation, list traversals, and binary file handling.
This guide breaks down the core Python programming patterns that reliably appear in board papers year after year under the current NCERT and CBSE guidelines.
1. Text File Operations: Counting and Filtering
File handling questions carry heavy weightage in the practical and theory papers. Examiners frequently test your ability to read text files, process lines or words, and apply conditional logic.
Common Question Pattern
Write a function in Python to read lines from a text file data.txt and display those words that start with a vowel or have a length greater than 5.
Solution Approach
def process_text_file():
try:
with open("data.txt", "r") as file:
line = file.readline()
while line:
words = line.split()
for w in words:
if w[0].lower() in "aeiou" and len(w) > 5:
print(w)
line = file.readline()
except FileNotFoundError:
print("File not found.")
Pro-Tip for Exams
Always use the with open(...) context manager. It automatically closes the file, preventing resource leak penalties. Always wrap file operations in try-except blocks to handle missing files gracefully.
2. Binary Files: pickle Module Operations
Binary file questions test your ability to serialize and deserialize complex data structures like dictionaries and lists using the pickle module.
Common Question Pattern
Create a function add_record() to append student records (roll number, name, marks) into a binary file student.dat, and another function search_record(rno) to search for a specific roll number.
Solution Approach
import pickle
def add_record():
with open("student.dat", "ab") as f:
rno = int(input("Enter Roll No: "))
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
record = [rno, name, marks]
pickle.dump(record, f)
def search_record(target_rno):
found = False
try:
with open("student.dat", "rb") as f:
while True:
record = pickle.load(f)
if record[0] == target_rno:
print(
f"Found! Name: {record[1]}, Marks: {record[2]}"
)
found = True
break
except EOFError:
if not found:
print("Record not found.")
Common Pitfalls to Avoid
- Forgetting to catch
EOFErrorwhen reading binary files using awhile Trueloop. - Using
"w"or"r"modes instead of"wb"or"rb".
3. Stack Implementation using Lists
Data structures questions require you to implement a Stack (LIFO principle) using Python lists with append() and pop() operations.
Common Question Pattern
Write a program to push and pop dictionaries containing book ID and book titles into a stack, filtering books priced above a certain threshold.
Solution Approach
def push_book(stack, book):
stack.append(book)
def pop_book(stack):
if len(stack) == 0:
return "Underflow"
else:
return stack.pop()
When evaluating stack questions involving user-defined functions, ensure you maintain a global or passed reference of the list acting as the stack and check for underflow conditions before every pop() operation.
4. String and List Manipulation Patterns
Basic logic questions test your string slicing, list comprehension, and argument mutation skills.
Common Question Pattern
Write a function that accepts a list of strings and replaces every string of odd length with its reverse, while keeping even-length strings unchanged.
Solution Approach
def modify_strings(lst): for i in range(len(lst)): if len(lst[i]) % 2 != 0: lst[i] = lst[i][::-1] return lst
Summary of High-Weightage Areas
| Topic | Common Question Type | Key Functions / Modules |
| :--- | :--- | :--- |
| Text Files | Word/character counting, searching | readline(), readlines(), split() |
| Binary Files | Object serialization/deserialization | pickle.dump(), pickle.load() |
| Data Structures | Stack push/pop operations | append(), pop() |
| CSV Files | Reading and writing tabular data | csv.reader(), csv.writer() |
Quick Checklist for CS Practical and Theory Exams
- [ ] Did you import all necessary modules (
pickle,csv,math) at the top of your script? - [ ] Are all loops reading files safely terminated using
EOFErroror line checks? - [ ] Did you handle empty data structures before executing pop or index operations?
- [ ] Are variable names meaningful and compliant with Python identifier rules?
Consistent practice of these four pillars—Text files, Binary files, Stacks, and List/String manipulations—will secure top marks in your Class 12 Computer Science evaluation. Keep coding, keep revising with Edumath!
