Edumath by Sereddy logo

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

3 August 2026 · Yesunadhareddy SereddyComputer ScienceClass 12PythonCBSE Board Exams

Board exams test your core logic, syntax accuracy, and output-tracing skills. In the Class 12 Computer Science paper, coding questions carry substantial weight. Examiners repeatedly draw from a specific set of concept blueprints year after year. Let us break down the exact categories of Python programs you must master to secure a full score.

1. Text and Binary File Handling Programs

File handling is non-negotiable in the CBSE Class 12 curriculum. Questions involving text files (.txt) and binary files (using the pickle module) appear in almost every alternative set of the board paper.

Reading and Filtering Text Files

You must be fluent in reading line-by-line, counting specific characters, words, or lines, and copying data conditionally.

# Program to count lines starting with 'A' or 'a' in a text file
def count_lines():
    file = open("story.txt", "r")
    count = 0
    for line in file:
        if line[0] in "Aa":
            count += 1
    print("Total lines:", count)
    file.close()

Binary File Operations with pickle

Binary file questions almost always test pickle.dump() and pickle.load() inside a try-except block to handle EOF errors.

import pickle

# Program to write student records to a binary file
def write_records():
    file = open("student.dat", "wb")
    while True:
        rno = int(input("Enter Roll No: "))
        name = input("Enter Name: ")
        record = [rno, name]
        pickle.dump(record, record)
        
        choice = input("Add more? (y/n): ")
        if choice.lower() == 'n':
            break
    file.close()

# Program to search a record in a binary file
def search_record(target_rno):
    file = open("student.dat", "rb")
    found = False
    try:
        while True:
            rec = pickle.load(file)
            if rec[0] == target_rno:
                print("Record Found:", rec[1])
                found = True
                break
    except EOFError:
        pass
    finally:
        file.close()
    if not found:
        print("Record not found.")

2. Stack Implementation Using Lists

Data structures form a major section of the theory and practical evaluation. Implementing a Stack (LIFO - Last In, First Out) using Python lists is a recurring 4-mark question.

| Operation | Python List Method / Logic | Description | | :--- | :--- | :--- | | Push | stack.append(element) | Adds an element to the top of the stack | | Pop | stack.pop() | Removes and returns the top element | | Peek/Display | Print list in reverse or loop | Views the top element without removing it |

# Stack implementation for storing book names
def isEmpty(stk):
    return len(stk) == 0

def Push(stk, item):
    stk.append(item)
    print(f"'{item}' pushed successfully.")

def Pop(stk):
    if isEmpty(stk):
        print("Underflow! Stack is empty.")
    else:
        item = stk.pop()
        print(f"Popped item: {item}")

def Display(stk):
    if isEmpty(stk):
        print("Stack is empty.")
    else:
        print("Stack elements (top to bottom):")
        for i in range(len(stk)-1, -1, -1):
            print(stk[i])

3. CSV File Manipulation

With the inclusion of the csv module in the syllabus, questions requiring reading and writing tabular data have become standard.

import csv

# Program to write user data into a CSV file
def write_csv():
    with open("emp.csv", "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(["EmpID", "Name", "Salary"])
        writer.writerow([101, "Aarav", 55000])
        writer.writerow([102, "Diya", 62000])

# Program to read and display a CSV file
def read_csv():
    with open("emp.csv", "r") as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)

4. User-Defined Functions Manipulating Pass-by-Reference

Board examiners love testing your understanding of mutable data types inside functions—specifically lists and dictionaries. You may be asked to write a function that shifts elements, swaps data, or updates values based on a condition.

# Program to interchange elements of even and odd positions in a list
def swap_alternate(lst):
    for i in range(0, len(lst)-1, 2):
        lst[i], lst[i+1] = lst[i+1], lst[i]
    return lst

numbers = [10, 20, 30, 40, 50, 60]
print("Original:", numbers)
print("Modified:", swap_alternate(numbers))

Quick checklist

  • [ ] Always close files explicitly using .close() or use the with open() context manager.
  • [ ] Wrap pickle.load() inside a try-except EOFError block to prevent runtime crashes.
  • [ ] Check for stack Underflow before executing a pop() operation.
  • [ ] Include newline='' when opening CSV files in write mode to avoid blank rows in Windows.
  • [ ] Double-check indentation, case sensitivity (True, False, None), and colon : placement.