Edumath by Sereddy logo

Study Blog

Daily tips & exam updates

Computer Science Class 12: Python Programs That Repeat in Board Exams

20 September 2026 · Yesunadhareddy SereddyClass 12 Computer SciencePythonCBSE Board ExamsEdumath

Scoring a 90+ in CBSE Class 12 Computer Science requires more than just knowing syntax. The programming section tests your ability to manipulate data structures, work with text files, and handle databases. An analysis of past board papers shows that examiners repeat specific logic patterns year after year with minor changes in wording.

If you master these core patterns, you can handle almost any coding question thrown at you in the theory exam. Here are the most recurring Python program types, complete with standard templates and tips to avoid losing marks.

1. Text File Operations: Counting and Filtering

File handling questions carry significant weight in the practical and theory papers. The most common format asks you to read a text file named matter.txt and perform conditional checks on lines, words, or characters.

Common Question Pattern

Write a function in Python to count how many lines in a text file start with the letter 'A' or 'a' (case-insensitive), or write a function to display all lines that contain a specific word like "Python".

Standard Solution Template

def count_a_lines():
    try:
        with open("matter.txt", "r") as file:
            count = 0
            for line in file:
                # Strip leading whitespaces to check the first character accurately
                cleaned_line = line.lstrip()
                if cleaned_line and cleaned_line[0] in "Aa":
                    count += 1
            print(f"Total lines starting with A/a: {count}")
    except FileNotFoundError:
        print("Error: The file does not exist.")

count_a_lines()

Board Exam Trap

Forgetting to handle the FileNotFoundError or missing the file.close() statement when not using the with context manager. Always use with open(...) to keep your code clean and automatically managed.

2. Binary File Operations: Using Pickle

Binary file handling using the pickle module is a favorite topic for 3-mark or 4-mark coding questions. You must know how to append records (usually dictionaries or lists) and search through them.

Common Question Pattern

Write a function to add student records (RollNo, Name, Marks) into a binary file student.dat, and another function to search and display details based on a given RollNo.

Standard Solution Template

import pickle

def add_record():
    with open("student.dat", "ab") as file:
        roll = int(input("Enter Roll No: "))
        name = input("Enter Name: ")
        marks = float(input("Enter Marks: "))
        record = [roll, name, marks]
        pickle.dump(record, file)

def search_record(rno):
    found = False
    try:
        with open("student.dat", "rb") as file:
            while True:
                record = pickle.load(file)
                if record[0] == rno:
                    print(f"Found! Name: {record[1]}, Marks: {record[2]}")
                    found = True
                    break
    except EOFError:
        pass
    
    if not found:
        print("Record not found.")

Board Exam Trap

Failing to catch the EOFError when reading a binary file in a while True loop will result in an abrupt program termination and loss of marks.

3. Stack Implementation Using Lists

Data structures questions test your understanding of LIFO (Last In, First Out) operations. In Python, lists serve as efficient stacks using append() for push and pop() for pop operations.

Common Question Pattern

Write a function PushBook(Stk, Book) and PopBook(Stk) where Stk is a list containing dictionaries of books [BookNo, BookName].

Standard Solution Template

def PushBook(Stk, book):
    Stk.append(book)
    print("Inserted successfully")

def PopBook(Stk):
    if len(Stk) == 0:
        print("Stack Underflow")
    else:
        item = Stk.pop()
        print(f"Popped item: {item[1]}")

4. SQL-Python Connectivity

Python-MySQL integration questions test both your database concepts and Python programming flow. You are often asked to write a program that fetches records meeting a specific criterion.

Common Question Pattern

Write a Python program to connect to a MySQL database named School and display all records from a table Student where marks are greater than 75.

Standard Solution Template

import mysql.connector as sql

def display_toppers():
    # Establish connection
    mydb = sql.connect(
        host="localhost",
        user="root",
        password="your_password",
        database="School"
    )
    
    cursor = mydb.cursor()
    query = "SELECT * FROM Student WHERE Marks > 75"
    
    cursor.execute(query)
    records = cursor.fetchall()
    
    for row in records:
        print(f"Roll No: {row[0]}, Name: {row[1]}, Marks: {row[2]}")
        
    cursor.close()
    mydb.close()

Quick Checklist for Python Coding Questions

| Component | What Examiners Look For | Common Mistake | | :--- | :--- | :--- | | File Handling | Proper opening modes ('r', 'w', 'rb') and closing files | Forgetting try-except blocks for missing files or EOF | | Pickle Module | Importing pickle, using dump() and load() correctly | Not handling EOFError during sequential reads | | Stacks | Checking for Underflow (len(Stk) == 0) before popping | Using pop(0) instead of pop(), which makes it a Queue | | SQL Connectivity | Creating cursor objects and executing fetchall() | Forgetting to close the cursor and database connection |

Practice writing these four patterns on paper without relying on auto-complete editors. In the board exam, neat indentation, proper variable names, and handling edge cases like empty files or empty stacks will secure your full marks.