Edumath by Sereddy logo

Study Blog

Daily tips & exam updates

Computer Science Class 12: Python Programs That Repeat in Exams

2 October 2026 · Yesunadhareddy SereddyComputer SciencePythonClass 12Board Exams

Class 12 Computer Science board exams do not test your ability to build complex software. They test your command over core algorithmic logic, data structure manipulation, and syntax precision under time pressure. Over the past several years of analyzing board papers, certain Python coding patterns appear with high frequency.

Mastering these repetitive problem types guarantees a solid score in the programming section. Here is a breakdown of the exact Python programs you must practice until you can write them without errors on paper.

1. Text File Operations: Reading and Filtering

File handling is a heavy-weight section in the board exam. Questions typically ask you to read a text file and perform specific string operations, such as counting lines, searching for words, or swapping characters.

Frequently Asked Pattern: Count Specific Lines or Words

You will often be asked to count how many lines in a file start with a particular letter or how many times a specific word appears.

def count_target_words(filename, target):
    try:
        with open(filename, "r") as file:
            count = 0
            for line in file:
                # Split line into words to match exact words
                words = line.split()
                for word in words:
                    if word.lower() == target.lower():
                        count += 1
            return count
    except FileNotFoundError:
        return "File not found"


# Example Usage
# print(count_target_words('story.txt', 'the'))

Exam Tip: Always use with open(...) to manage file resources. It automatically closes the file, preventing loss of marks for bad file management.

2. Binary Files: pickle Module Operations

Binary file questions almost always involve the pickle module. You must know how to append (dump) records—usually a dictionary or a list containing roll numbers, names, and marks—and how to read (load) them back to search for a specific record.

Frequently Asked Pattern: Search a Record in a Binary File

import pickle


def search_student(rno):
    found = False
    try:
        with open("student.dat", "rb") as file:
            while True:
                # Unpickling until EOFError
                data = pickle.load(file)
                if data["RollNo"] == rno:
                    print(f"Found! Name: {data['Name']}, Marks: {data['Marks']}")
                    found = True
                    break
    except EOFError:
        pass
    except FileNotFoundError:
        print("Data file does not exist.")

    if not found:
        print("Record not found.")

Exam Paper Trap: Forgetting to wrap pickle.load() inside a while True loop with an EOFError exception handler will cost you immediate marks.

3. Data Structures: Stack Implementation Using Lists

The syllabus requires implementing a Stack (LIFO) using Python lists. Questions range from basic push and pop operations to filtering data based on conditions (e.g., push employee names with lengths greater than 5).

Frequently Asked Pattern: Push and Pop with Object-Type Records

def push_employee(stack, emp_record):
    stack.append(emp_record)
    print("Pushed successfully.")


def pop_employee(stack):
    if len(stack) == 0:
        return "Underflow"
    else:
        return stack.pop()


def display_stack(stack):
    if len(stack) == 0:
        print("Stack is empty")
    else:
        # Traverse from top to bottom
        for i in range(len(stack) - 1, -1, -1):
            print(stack[i])

4. List and String Manipulation Functions

You will frequently be asked to write a function that modifies a list in-place or returns a modified version based on index manipulation.

Frequently Asked Pattern: Swap Even and Odd Index Elements

def swap_alternate(arr):
    n = len(arr)
    # If length is odd, loop up to n-1
    for i in range(0, n - 1, 2):
        arr[i], arr[i + 1] = arr[i + 1], arr[i]
    return arr

| Problem Domain | Common Examiner Expectation | Typical Penalty for Error | | :--- | :--- | :--- | | Text Files | Proper use of readline() or loops over file objects | Missing file close or syntax error in path | | Binary Files | Correct handling of EOFError | Infinite loops or crashing at end-of-file | | Stack Operations | Checking for Underflow before pop() | Runtime errors during evaluation | | Functions | Proper return statements and parameter passing | Returning None accidentally |

Quick Checklist

Before walking into your board exam hall, make sure you can write these standard structures from memory:

  • Opening files in r, w, rb, and wb modes without syntax errors.
  • Using pickle.dump() and pickle.load() inside proper exception blocks.
  • Writing a standard Stack Push and Pop function with an Underflow check.
  • String slicing and traversal using negative indices (S[−1]S[-1]).
  • Random number generation using the random module (random.randint(a, b), random.random()).