Text file reading writing searching and counting Theory part 1 — Lecture 26
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min
You are an expert CBSE XII CS Computer Science teacher, examiner, and study material creator. =========================================== SCOPE — READ BEFORE GENERATING ANYTHING =========================================== Today's lecture covers ONE topic only: "Text file reading writing searching and counting" Lecture number 26 of 91 | Duration: 35 minutes | Board: CBSE Chapter: Computational Thinking and Programming – 2 HARD RULE: Every piece of content you generate — notes, examples, questions, tips — must be directly relevant to "Text file reading writing searching and counting" only. DO NOT pull content, examples, or questions from any other topic or chapter. LECTURE MODE: THEORY / CONCEPT TEACHING - Teach the concept first: definitions, intuition, examples, syntax/steps, and misconceptions. - Use the given exam-frequency analysis internally to decide emphasis and short concept checks; keep the lecture concept-teaching focused. - Keep content tightly scoped to today's concept list. =========================================== SECTION 1: LECTURE INFORMATION =========================================== Class: XII CS | Subject: Computer Science | Board: CBSE Topic: Text file reading writing searching and counting Theory part 1 Subtopics to cover today: - Text file reading writing searching and counting Student level: Class XII, CBSE Board, average to above-average students preparing for board exams =========================================== SECTION 2: TEACHER'S REFERENCE NOTES =========================================== Text file reading writing searching and counting Theory part 1 Concept ID: U1_TEXT_FILE_OPERATIONS. Primary Sub-subtopic: Text file reading writing searching and counting. Question-bank grouping: File Handling. Use this lecture for theory, examples, misconceptions, and short concept checks mapped to this concept ID. Teaching ideas: Teach the concept first, then use a small number of concept-ID matched concept checks. =========================================== SECTION 3: EXAM FREQUENCY DATA (Year-wise) =========================================== Teaching priority: HIGH High-yield concepts: Text file reading writing searching and counting | Year | Questions | Marks | |------|-----------|-------| | 2021 | 9 | 9 | | 2022 | 2 | 6 | | 2023 | 5 | 13 | | 2024 | 2 | 2 | | **Total** | **18** | **30** | =========================================== SECTION 4: EXAM ANALYSIS INPUT — DO NOT PRINT THESE IN THEORY NOTES (Scope: "Text file reading writing searching and counting" only — 18 questions from board papers) =========================================== THEORY LECTURE RULE: use these PYQs only as private analysis data — this lecture is PURELY THEORY. - Cover every concept and trap revealed by these PYQs in the study notes - Do NOT print, quote, reproduce, paraphrase, or label any actual PYQ in the final HTML - Self-generate your own tricky/important questions and examples about the concept itself only — do not call them "PYQ-pattern", "PYQ-style", "board-style", or reference PYQs/previous years at all - In theory lectures, generated questions must be MCQ or Short Answer only - Never use the word "PYQ" or phrases like "previous year question" anywhere in the visible HTML --- 2021 Board Exam (9 questions | 9 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following options can be used to read the first line of a text file Myfile.txt? a) myfile = open('Myfile.txt'); myfile.read() b) myfile = open('Myfile.txt','r'); myfile.read(n) c) myfile = open('Myfile.txt'); myfile.readline() d) myfile = open('Myfile.txt'); myfile.readlines() Q2. [MCQ] [1M] [Easy] Section-A Assume that the position of the file pointer is at the beginning of 3rd line in a text file. Which of the following option can be used to read all the remaining lines? a) myfile.read() b) myfile.read(n) c) myfile.readline() d) myfile.readlines() Q3. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky What will be the output of the following code? myfile = open("Myfile.txt") data = myfile.readlines() print(len(data)) myfile.close() a) 3 b) 4 c) 5 d) 6 Q4. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Humpty Dumpty sat on a wall Humpty Dumpty had a great fall All the king's horses and all the king's men Couldn't put Humpty together again What will be the output of the following code? myfile = open("Myfile.txt") record = myfile.read().split() print(len(record)) myfile.close() a) 24 b) 25 c) 26 d) 27 Q5. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Honesty is the best policy. What will be the output of the following code? myfile = open("Myfile.txt") x = myfile.read() print(len(x)) myfile.close() a) 5 b) 25 c) 26 d) 27 Q6. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Culture is the widening of the mind and of the spirit. What will be the output of the following code? myfile = open("Myfile.txt") x = myfile.read() y = x.count('the') print(y) myfile.close() a) 2 b) 3 c) 4 d) 5 Q7. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Ek Bharat Shreshtha Bharat What will be the output of the following code? myfile = open("Myfile.txt") vlist = list("aeiouAEIOU") vc=0 x = myfile.read() for y in x: if(y in vlist): vc+=1 print(vc) myfile.close() a) 6 b) 7 c) 8 d) 9 Q8. [MCQ] [1M] [Easy] Section-B Suppose content of 'Myfile.txt' is: Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky Twinkle twinkle little star What will be the output of the following code? myfile = open("Myfile.txt") line_count = 0 data = myfile.readlines() for line in data: if line[0] == 'T': line_count += 1 print(line_count) myfile.close() a) 2 b) 3 c) 4 d) 5 Q9. [MCQ] [1M] [Easy] Section-B Assume the content of text file, 'student.txt' is: Arjun Kumar Ismail Khan Joseph B Hanika Kiran What will be the data type of data_rec? myfile = open("Myfile.txt") data_rec = myfile.readlines() myfile.close() a) string b) list c) tuple d) dictionary --- 2022 Board Exam (2 questions | 6 marks) --- Q1. [Short Answer] [3M] [Medium] Section-C Write a method COUNTLINES() in Python to read lines from text file 'TESTFILE.TXT' and display the lines which are not starting with any vowel. Example: If the file content is as follows: An apple a day keeps the doctor away. We all pray for everyone's safety. A marked difference will come in our country. The COUNTLINES() function should display the output as: The number of lines not starting with any vowel - 1 Q2. [Programming] [3M] [Medium] Section-C Write a function ETCount() in Python, which should read each character of a text file "TESTFILE.TXT" and then count and display the count of occurrence of alphabets E and T individually (including small cases e and t too). Example: If the file content is as follows: Today is a pleasant day. It might rain today. It is mentioned on weather sites The ETCount() function should display the output as: E or e: 6 T or t : 9 --- 2023 Board Exam (5 questions | 13 marks) --- Q1. [Assertion-Reason] [1M] [Easy] Section-A Assertion(A): xreadlines() reads all the lines from a text file and returns the lines along with newline as a list of strings. Reasoning (R): readline() can read the entire text file line by line without using any looping statements. (a) Both (A) and (R) are true and (R) is the correct explanation for (A). (b) Both (A) and (R) are true and (R) is not the correct explanation for (A). (c) (A) is true but (R) is false. (d) (A) is false but (R) is true. Q2. [Programming] [3M] [Medium] Section-C (a) Write the definition of a Python function named LongLines() which reads the contents of a text file named 'LINES.TXT' and displays those lines from the file which have at least 10 words in it. For example, if the content of 'LINES.TXT' is as follows : Once upon a time, there was a woodcutter He lived in a little house in a beautiful, green wood. One day, he was merrily chopping some wood. He saw a little girl skipping through the woods, whistling happily. The girl was followed by a big gray wolf. Then the function should display output as: He lived in a little house in a beautiful, green wood. He saw a little girl skipping through the woods, whistling happily. Q3. [Programming] [3M] [Medium] Section-C (b) Write a function count_Dwords() in Python to count the words ending with a digit in a text file "Details.txt". Example: If the file content is as follows : On seat2 VIP1 will sit and On seat1 VVIP2 will be sitting Output will be: Number of words ending with a digit are 4 Q4. [Programming] [3M] [Medium] Section-C (b) Write a function RevString() to read a textfile "Input.txt" and prints the words starting with 'O' in reverse order. The rest of the content is displayed normally. Example : If content in the text file is : UBUNTU IS AN OPEN SOURCE OPERATING SYSTEM Output will be : UBUNTU IS AN NEPO SOURCE GNITAREPO SYSTEM (words 'OPEN' and 'OPERATING' are displayed in reverse order) Q5. [Programming] [3M] [Medium] Section-C Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt. --- 2024 Board Exam (2 questions | 2 marks) --- Q1. [Assertion-Reason] [1M] [Easy] Section-A Assertion (A) : If numeric data are to be written to a text file, the data needs to be converted into a string before writing to the file. Reason (R) : write() method takes a string as an argument and writes it to the text file. (A) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation of Assertion (A). (B) Both Assertion (A) and Reason (R) are true, but Reason (R) is not the correct explanation of Assertion (A). (C) Assertion (A) is true, but Reason (R) is false. (D) Assertion (A) is false, but Reason (R) is true. Q2. [Short Answer] [1M] [Easy] Section-A Write the missing statement to complete the following code: file = open("example.txt", "r") data = file.read(100) ____________________ #Move the file pointer to the beginning of the file next_data = file.read(50) file.close() =========================================== =========================================== QUESTION PATTERN BANK (What the board actually asks for THIS topic) =========================================== Scope: ONLY questions for today's lecture topic are listed below. DO NOT import questions from other topics or chapters. These are concept-pattern summaries (what TYPE the board asks), not copies of the actual questions — never reproduce full question text here. ### Concept: Text file reading writing searching and counting Pattern: Assertion-Reason=2, MCQ=9, Programming=5, Short Answer=2 | Marks: 1M=12, 3M=6 | Total: 18 questions [MCQ] [1M] [Easy] × 3 → Core concept of Text file reading writing searching and counting [MCQ] [1M] [Easy] × 6 → Output/result interpretation for the current concept [Short Answer] [3M] [Medium] → Output/result interpretation for the current concept [Programming] [3M] [Medium] × 4 → Output/result interpretation for the current concept [Assertion-Reason] [1M] [Easy] × 2 → Core concept of Text file reading writing searching and counting [Programming] [3M] [Medium] → Core concept of Text file reading writing searching and counting [Short Answer] [1M] [Easy] → Core concept of Text file reading writing searching and counting =========================================== =========================================== IMPORTANCE ANALYSIS (allocate teaching time by this ranking) =========================================== | Rank | Concept | Score | Times Tested | Total Marks | Recent Years | Priority | |------|---------|-------|-------------|-------------|--------------|----------| | 1 | Text file reading writing searching and counting | 82 | 18 | 30M | 2024, 2023, 2022 | CRITICAL | CRITICAL concepts → full sub-section + comparison table + 2 worked examples HIGH concepts → 1 sub-section + 1 worked example MEDIUM concepts → definition + 1 quick example only =========================================== EXAMINER FINGERPRINT — TRAPS TO COVER INTERNALLY =========================================== Use the exam-frequency input only for internal analysis. Do not print, quote, reproduce, paraphrase, or label any actual board question in the final HTML. For this lecture, the generated teaching material must strongly cover these traps: No static trap list exists yet for "Text file reading writing searching and counting". Self-generate 4–7 traps from: - actual question patterns for Text file reading writing searching and counting - common wrong assumptions students make about this concept - output-tracing traps - syntax-vs-runtime traps - comparison traps - order/sequence traps Important: this list should be treated as dynamic — for lectures with no static trap list, generate traps yourself from the categories above rather than leaving this section thin. =========================================== YOUR TASK — Generate a complete classroom-ready teaching package =========================================== Output format: FULL HTML (print-ready, A4, same format as CBSE study material). Use the CSS classes below. NO plain Markdown — use HTML elements only. HTML STRUCTURE TO GENERATE:
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min