0% found this document useful (0 votes)
5 views

Text files worksheet in python

This document is a test paper for Class XII Computer Science, consisting of 5 sections with a total of 30 marks. Each section contains questions of varying marks, covering topics related to file handling in Python. The test includes practical coding questions, theoretical differences, and function writing tasks.

Uploaded by

mage
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Text files worksheet in python

This document is a test paper for Class XII Computer Science, consisting of 5 sections with a total of 30 marks. Each section contains questions of varying marks, covering topics related to file handling in Python. The test includes practical coding questions, theoretical differences, and function writing tasks.

Uploaded by

mage
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SHREE NIKETAN PATASALA

CLASS:XII S / CYCLE TEST 3 /COMPUTER SCIENCE(083)


DURATION: 1 HR MARKS:30
Date:17/04/2025
GENERAL INSTRUCTIONS:
● The paper is divided into 5 Sections- A, B, C, D and E.
● Section A, consists of 7 questions (1 to 7). Each question carries 1 Mark.
● Section B, consists of 4 questions (8 to 11). Each question carries 2 Marks.
● Section C, consists of 2 questions (12 and 13). Each question carries 3 Marks.
● Section D, consists of 1 question (14). Each question carries 4 Marks.
● Section E, consists of 1 question (15). Each question carries 5 Marks.
Q.No SECTION-A (7 x 1 = 7 Marks) MARKS

1 A file “MYDATA.TXT” is opened as 1


file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.

2 Write a Python statement to open a text file “DATA.TXT” so that new contents 1
can be written on it.

3 Consider the following python statement : 1


f=open(‘content.txt’)
Which of the following is an invalid statement in python
a)F.seek(1,0) b)F.seek(0,1) c)F.seek(0,-1) d)F.seek(0,2)

4 To read three characters from file object f , we use 1


(a) f.read(3) (b) f.read( ) (c) f.readline( ) (d) f.readlines( )

5 Which of the following statements is used to ensure that a file is automatically closed 1
after its operations are completed, even if errors occur?
(a) file.close( ) (b) try...except...finally
(c) with open(...) as file: (d) import file handling

6 What will be the output of the following Python code snippet if the file "my_file.txt" 1
contains the line "Hello World!"?
file = open("my_file.txt", "r")
content = file.read(5)
print(content)
file.close()

7 What will f.write("Hello\nWorld") write into the file? 1


Q.No SECTION-B (4 x 2 = 8 Marks) MARKS
8 What is the difference between file opening mode “a” and “w” ? 2

9 Considering the content stored in file “WORLDCUP.TXT”, write the output 2


” India won the Cricket world cup of 1983 “
f = open(“WORLDCUP.TXT”)
print(f.read(2))
print(f.read(2))
print(f.read(4))

10 Write a function COUNTSIZE( ) in Python to read the file “POEM.TXT” and display
size of file. 2
OR
What is the output of the following code?
fh=file(“poem.txt”)
size=len(fh.read( ))
print(fh.read(5))

11 What is the difference between readline( ) and readlines( ) function? 2


Q.No SECTION-C (2 x 3 = 6 Marks) MARKS
12 Write the output of following statements
f = open("CORONA.TXT") 3
sr1 = ____________ # to read first line of file
str2 = ___________ # to read next line of file
str3 = ___________ # to read remaining lines of file

13 Write a user – defined function countH() in Python that displays the number 3
of lines starting with ‘H’ in the file ‘Para.txt”. Example , if the file contains:

Whose woods these are I think I know.


His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
OR

Write a function countmy() in Python to read the text file “DATA.TXT” and
count the number of times “my” occurs in the file. For example , if the file
“DATA.TXT” contains –
“This is my website. I have displayed my preference in the CHOICE
section.” The country( ) function should display the output as:
“my occurs 2 times”
Q.No SECTION-D (1x 4 = 4 Marks) MARKS
14 a)Read the code and answer the question
fh=open(“main.txt”,”w”) 2
fh.write()
fh.close()

If the file contains “GOOD” before execution ,what will be the contents of the file
after execution of this code.

b)If a text file has contains the following data.predict the output of the code given 2
below
I SELL COMPUTERS.
I HAVE A COMPUTER.
I NEED A COMPUTER

fin=open(“data.txt”,’r’)
line=fin.readlines()
print(“Last line=”,line[-1])
Q.No SECTION-E (1x 5 = 5 Marks) MARKS
15 a)Write a function in python to count the number of lines in “POEM.txt” that begins 3
with an Uppercase character.
OR
Write a function in python to count the number of lines in a text file starting with
letter ‘A’

b)Complete the missing statement using for loop to print all the lines of file

f = open(“EMP.TXT”) 2
for ______________________ :
print(______)

You might also like