CS12_DFH_TEXT

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

XII – COMPUTER SCIENCE MARKS : 20

WORK SHEET – (DATA FILE HANDLING – TEXT FILES )


Answer all the questions .
1. In f=open(“data.txt”, “r”), r refers to ______. 1
a. File handle b. File object c. File Mode d Buffer

2. Suppose content of ‘mytext.txt’ file is: 1


"The key to success is focus on goals,not obstacles."
What will be the output of the following code?
file = open("mytext.txt","r")
txt = file.read()
print(file.read(10))
file.close()
(a) The key to (b) obstacles. (c) Error (d) No Output

3. To read the 4th line from the text file, which of the following statements is true? 1
a. dt = f.readlines();print(dt[3]) b. dt=f.read(4) ;print(dt[3])
c. dt=f.readline(4);print(dt[3]) d. All of these

4. Suppose content of 'Myfile.txt' is: 1


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

5. Suppose the content of "Rhymes.txt" is: 1


Baa baa black sheep,
Have you any wool?
What will be the output of the following code?'''
F=open("Rhymes.txt")
S=F.read()
L=S.split()
for i in L:
if len(i)%3!=0:
print(i,end=" ")
F.close()
(a) Baa baa you any (b) black have wool? (c) black sheep, have wool? (d) Error

6. Write a function countVowels() in Python, which should read each character of a text file myfile.txt”, count the number
of vowels and display the count. 3
Example: If the “myfile.txt” contents are as follows:
This is my first class on Computer Science.
The output of the function should be: Count of vowels in file: 10

8. Write a function DISPLAYWORDS( ) in python to display the count of words starting with “t” or “T”in a text file
‘STORY.TXT’. 3

9. ka win of class 12 is asked by his Computer Science teacher to count the number of “the” and “and” in a text file
“Book.txt”. He was able to do the task to some extent, but was confused in some areas. Help him out to complete the task.
def displaytheand():
num=0
_________ #Statement 1
N=f.read()
M=N.split()
for x in M:
if x=="the" or x== "and":
print(x)
num=num+1
__________ #Statement 2
print("Count of the/My in and:",num)

1. Which of the following is the correct one to be used in the place of statement 1 1
a) f=open("Book.txt","w") b) f=open("Book.txt","r")
c) f=open("Book.txt","a") d) F=open("Book.txt","r")
2. Identify the correct statement that may be used in the place of statement 2. 1
a) f.close() b) f.close(“book.txt”)
c) close() d) None of the above

7. Suppose content of 'Myfile.txt' is 2


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()

9. Kum.Anisha wants to develop a program to count the number of “Me” or “My” words present in a text file
“STORY.TXT” . But she is not sure about the code . Help her to complete it. 5
def displayMeMY():
n=0
f=open(“story.txt” , „____‟) #line 1
N = __read() #line2
M = N.________ #line 3
for x in M :
if ____________________: #line 4
n=n+1
f.________ #line 5 Closing the file.
print(“Count of Me /My words : “, ____) #line 6

i. Which access mode to be used in line 1 to open the file .


a) w b) r c) rb d) a
ii. Fill the line 2 by suitable option
a) F. b) f. c) n. d) N.
iii. Which method to be used to complete the line 3
a) readline() b) split() c) write() d) writelines()
iv. select the correct option to complete the line 4
a) x==me or x== my c) x==”Me” or x==”My”
b) x==”me” or “my” d) x==[“Me”,”My”]
v. Which function is used to complete line 5.
a) Exit() b) close() c) end() d) off()

You might also like