Standard difficulty · 20 questions · 40 marks · Time: 43 minutes
Show your working for Sections B and C. Answers are on the last page — try every question first!
File Handling in Python · AW-12CS-FIL-2026-V1 · For parents — fold back or tear off before handing the sheet to your child.
| Q | Answer | Working |
|---|---|---|
| 1 | (d) 'w' | — |
| 2 | (a) 1 | count the lines starting with T |
| 3 | (d) 17 | split() breaks at spaces and newlines |
| 4 | (a) 15 | 'w' empties the file first, then writes 5 + 2 × 5 characters (each \n is one) |
| 5 | (d) append and read; creates the file if needed | 'a+': append and read; creates the file if needed |
| 6 | (c) 2 | records with marks above the limit: Priya, Dev |
| 7 | (d) Rohan | int() turns the score text into a number for the comparison |
| 8 | (c) 5 | tell() gives the position after the characters already read |
| 9 | A text file holds characters that people can read, in lines ending with newline characters; a binary file holds raw bytes (such as pickled objects or images) that only programs read. | — |
| 10 | 1 | count the lines starting with S |
| 11 | (a) Bees; (b) 4; (c) ee | read(n) reads n characters; seek(k) moves to position k (counting from 0) |
| 12 | Boats#carry#fish#to#the#market Mangoes#ripen#in#summer Tea#is#ready#now | split() drops the spaces and the newline; join() puts # between the words |
| 13 | 33 | 'a' keeps the old 9 characters and adds 6 + 3 × 6 characters (each \n is one) |
| 14 | Ananya, Asha, 2 | load() reads one record each time; EOFError ends the loop |
| 15 | Priya | next(r) skips the header; int() turns the score text into a number |
| 16 | (a) 4; (b) Kabir,11,45 | writerows() writes one line per inner list, fields separated by commas: name,class,marks / Arjun,12,49 / Rohan,12,98 / Kabir,11,45 |
| 17 | Open with open('a.txt') as f: data = f.read(); for ch in data: if ch.isalpha(): count upper or lower with isupper(); if ch.lower() in 'aeiou' add to vowels, else to consonants. Print the four counts. | — |
| 18 | (a) 131; (b) 27; (c) 33; (d) 5 | 26 + 33 + 25 + 21 + 26 characters, each line counted with its newline |
| 19 | (a) 4 [4, 'Meera', 81]; (b) 'rb' reads every record into a list; after the update, 'wb' empties the file and writes all the records back. | roll 4: 77 + 4 |
| 20 | (a) 4, Meera 83; (b) the header row would be read as data, and int('score') would raise a ValueError. | highest score 83 |