Chapter - 5 Data File Handling - Text File Objective Answer Type Quesrtions 1 X 10 10 MARKS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter – 5 Data File Handling – Text File

OBJECTIVE ANSWER TYPE QUESRTIONS


1 X 10 = 10 MARKS
1. What is default mode for opening a file?
(a) a, (b) r, (c) w, (d) None of these.
2. Write down the significance of opening a file in raw mode.
3. Name the mode of opening a file in which reading and writing both are
possible and a new file opens if it does not exist.
(a) r+, (b)w+, (c)a+, (d) All of these
4. Advantage of opening a file in ‘with open’ construct:
(a)File reading done automatically (b) File writing done
automatically
(c) File closing done automatically (d) None of these
5. Choose the correct methods of opening a file:
(a) F = open(‘C:\python\p.txt’) (b) F = open(‘C:\\python\\
p.txt’)
(c)F= open(r’C:\python\p.txt’) (d) F = open(‘C:\python\
p.txt’,R)
6. Name the function which stores the list of strings in a file.
7. Write the output of the following code if the file ‘stock.txt’ stores the
contents
“Computer has no intelligence”:
F=open(‘stock.txt’)
fl.read(4)
print(fl.read(14))
8. What is the job of the following program?
f1=open('Question.txt','r')
f2=open('Answer'txt','r')
f3=open('QA.txt','w')
while True:
Q = f1.readine()
A = f2.readline()
if Q == “ “ or A == “ “:
break
f3.write(Q+A)
f1.close( )
f2.close( )
f3.close( )
9. Write the syntax to display the no. of characters stored in a text file
FTP.txt.
10. Choose the correct syntax for displaying the last line of a text file
containing minimum 5 lines:
(a) line = open("tax.txt",'r').read()
print(line[-1])
(b)line = open("tax.txt",'r+').readline()
print(line[4])
(c) line = open("tax.txt",'r').readlines()
print(line[-1])
(d)line = open("tax.txt",'r').read(-1)
print(line)
VERY SHORT ANSWER TYPE QUESRTIONS
2 X 5 = 10 MARKS
1. Write down the statement in python to open a raw text file
“Question.txt” in reading and writing mode only if file exists.
2. Write down the statement in python to open a binary file
“sound.bin” in reading and appending mode stored in the path “C:\
MyFiles\Python\”.
3. Consider the file “Requirement.txt” contains the following:
You are welcome to 10 Russell Street.
Come at 12 AM.
Find output of the following code:
for L in file("Requirement.txt"):
word=L.split()
for x in word:
if x.islower() == True:
print(x, end=’:’)
4. Write a program in python to count the number of lines present in a
text file “Tp.txt”.
5. State the purpose of using flush() for dealing with files in python.

SHORT ANSWER TYPE QUESRTIONS


3 X 5 = 15 MARKS
1. Differentiate between readline() and readlines() method along with
example for each.
2. Differentiate between write() and writelines() method along with
example for each.
3. Write python statement to display the messages “Absolutely
correct” and “No Error” in the standard output device and standard
error device connected to file. Name the module to be imported if
any.
4. Re-write the following code after removing errors and underline the
corrections only:
ftp1 = open("D:\Main.txt",'a')
ftp2 = open("Secondary.txt")
for i in ftp2:
x = lstrip(i)
ftp1.writeLine(x)
5. Write a function in python with accepts a string and display the
number of occurrence of that string in a file “Pretext.txt”.
LONG ANSWER TYPE QUESRTIONS
4 X 5 = 20 MARKS
1. Write a function in Python to remove only those lines of the file
“Program.txt” which are starting from ‘#’.
2. Write a program in Python to count the number of vowels,
consonants, digits and white spaces present in a text file
“Document.txt”.
3. Write a program in Python to display the reverse of each line stored
in a file “Paragraph.txt”.
4. Write a function in Python to input First Name, Last Name and
Overall marks of 10 students from the user and store in a file
“Student.txt”. Display the full name of the student who is getting
highest percentage of marks.
5. Write a program in Python to display the number of lines starting
with ‘T’ or ‘I’ (both upper and lower case) in the file ‘Description.txt’.

You might also like