Edumath by Sereddy logo

Computer Science Class 12: Python programs that repeat in exams

9 August 2026 · Yesunadhareddy SereddyComputer ScienceClass 12PythonBoard Exams

Board exams test repetitive logic patterns in Python. Whether you are following the CBSE, ICSE, or state board curriculum for the 2026-27 academic session, the examiners rely on a fixed bank of concepts. If you master list manipulations, string parsing, user-defined functions, and file handling, you can easily secure full marks in the programming section.

Let us break down the exact Python program structures that appear year after year.

1. String Manipulation: Vowels, Spaces, and Case Swapping

String processing questions test your ability to traverse characters using loops and built-in string methods. Examiners frequently ask programs that count specific characters or alter case without using advanced libraries.

Common Question Template

Write a function process_string(s) that takes a string, counts the vowels, and returns a new string with all uppercase letters converted to lowercase and vice versa.

Solution Code

def process_string(s):
    vowels = "AEIOUaeiou"
    vowel_count = 0
    new_str = ""

    for char in s:
        if char in vowels:
            vowel_count += 1
        if char.isupper():
            new_str += char.lower()
        elif char.islower():
            new_str += char.upper()
        else:
            new_str += char  # Keep spaces and punctuation unchanged

    print(f"Total Vowels: {vowel_count}")
    return new_str


# Example usage
text = "Edumath 2026!"
print(process_string(text))

Exam Trap

Forgetting that strings in Python are immutable. You cannot do s[i] = 'a'. You must build a new string using concatenation or join methods.


2. List Operations: Swapping, Shifting, and Frequency

Lists form the backbone of data structures in Class 12 CS. Expect questions on finding the second largest element, shifting elements by kk positions, or swapping alternate elements.

Common Question Template

Write a Python function to accept a list of numbers and swap elements with their adjacent neighbors (index 0 with 1, 2 with 3, etc.). If the list has an odd number of elements, the last element remains untouched.

Solution Code

def swap_alternate(lst):
    length = len(lst)
    for i in range(0, length - 1, 2):
        lst[i], lst[i + 1] = lst[i + 1], lst[i]
    return lst


# Example usage
numbers = [10, 20, 30, 40, 50]
print("After swapping:", swap_alternate(numbers))

3. Working with Text Files: Reading, Writing, and Word Counting

Text file handling questions carry heavy weightage in the practical and theory papers. You must be comfortable with read(), readline(), readlines(), and write().

Common Question Template

Write a function count_words() that reads a text file named notes.txt and counts the total number of words starting with the letter 'M' or 'm'.

Solution Code

def count_m_words():
    count = 0
    try:
        with open("notes.txt", "r") as file:
            data = file.read()
            words = data.split()
            for w in words:
                if w[0].lower() == "m":
                    count += 1
        print(f"Total words starting with M/m: {count}")
    except FileNotFoundError:
        print("The file notes.txt does not exist.")


count_m_words()

4. Binary and CSV Files using pickle and csv

Examiners love testing binary file serialization and CSV parsing because they check syntax precision.

Binary File Template

import pickle


def write_binary():
    with open("students.dat", "wb") as f:
        # Dictionary of records
        data = {"RollNo": 101, "Name": "Yesunadhareddy", "Marks": 95}
        pickle.dump(data, f)


def read_binary():
    try:
        with open("students.dat", "rb") as f:
            while True:
                record = pickle.load(f)
                print(record)
    except EOFError:
        pass


write_binary()
read_binary()

Comparison of File Modes in Python Exams

| Mode | Operation | Pointer Position | Creates File if Missing? | Truncates Existing? | | :--- | :--- | :--- | :--- | :--- | | r | Read | Beginning | No | No | | w | Write | Beginning | Yes | Yes | | a | Append | End | Yes | No | | rb | Read Binary | Beginning | No | No | | wb | Write Binary | Beginning | Yes | Yes |


5. Stacks using Lists (Push and Pop)

Linear data structures implemented via lists are mandatory in the syllabus. You must write menu-driven or modular functions for Push and Pop.

Common Question Template

Write functions Push(Stk, item) and Pop(Stk) to implement a stack of numbers where the last element of the list acts as the top of the stack.

Solution Code

def isEmpty(stk):
    return len(stk) == 0


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


def Pop(stk):
    if isEmpty(stk):
        return "Underflow"
    else:
        item = stk.pop()
        return f"Popped item: {item}"


# Driver code
stack = []
Push(stack, 45)
Push(stack, 60)
print(Pop(stack))

Quick checklist for your CS exam

  • [ ] Did you handle EOFError when reading binary files using pickle.load() inside a while True loop?
  • [ ] Did you close files explicitly, or are you using the safe with open(...) context manager?
  • [ ] Are your function arguments matching the question paper's exact naming conventions?
  • [ ] Did you account for edge cases like empty lists, empty strings, or single-element inputs?