0% found this document useful (0 votes)
83 views21 pages

DATA FILE HANDLING Chapter Clearance

The document contains a comprehensive set of questions and tasks related to file handling in Python, specifically for a Chapter on Data File Handling for Class XII. It covers various aspects such as file modes, reading and writing files, and includes programming exercises to demonstrate these concepts. Additionally, it includes fill-in-the-blank and multiple-choice questions to assess understanding of file operations.

Uploaded by

tamilselvi721990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views21 pages

DATA FILE HANDLING Chapter Clearance

The document contains a comprehensive set of questions and tasks related to file handling in Python, specifically for a Chapter on Data File Handling for Class XII. It covers various aspects such as file modes, reading and writing files, and includes programming exercises to demonstrate these concepts. Additionally, it includes fill-in-the-blank and multiple-choice questions to assess understanding of file operations.

Uploaded by

tamilselvi721990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

MAHATMA

(Affiliated to Central Board of Secondary Education - New Delhi)


CHAPTER CLEARANCE
STD: XII CH 7- DATA FILE HANDLING

Questions:
1. What is the function used to open a file in Python?
2. What is a file mode in Python?
3. What is the difference between write and append?
4. What is the default mode for opening a file in Python?
5. How do you read the entire content of a file in Python?
6. What is the difference between read() and readline() in file handling?
7. How do you write data to a file in Python?
8. What happens if you open a file in 'w' mode and the file already exists?
9. How do you append data to an existing file in Python?
10.What does the close() method do in file handling?
11.What is the use of the with statement in file handling?
12.How do you read a file line by line in Python?
13.What is the use of the readlines() method in file handling?
14.Differentiate between text file and binary file.
15.Differentiate between readline() and readlines().
16.Differentiate between write() and writelines().
17.Write the use and syntax for the following methods:

a) open() b) read() c) seek() d) dump()

18.Write the file mode that will be used for opening the following files. Also, write the
Python statements to open the following files:

a) a text file “example.txt” in both read and write mode.


b) a binary file “bfile.dat” in write mode.
c) a text file “try.txt” in append and read mode.
d) a binary file “btry.dat” in read only mode.

19.What is the difference between the following set of statements (a) and (b):

P = open("practice.txt", "r")
P.read(10)
with open("practice.txt", "r") as P:
x = P.read()
20.Write a command(s) to write the following lines to the text file named hello.txt.
Assume that the file is opened in append mode.
“ Welcome my class”
“It is a fun place”
“You will learn and play”
21.Differentiate between file modes r+ and w+ with respect to python.
22. What would be the data type of variable data in the following statements?
(a) data = f.read()
(b) data = f.read(10)
(c) data = f.readline()
(d) data = f.readlines()

23. What is difference between tell() and seek() methods


24. Write a method in Python to read lines from a text file MYNOTES.TXT and display
those lines which start with the alphabet 'K'.
25. Write a function file_long() that accepts a filename and reports the file's longest line.
26. Write a function remove_lowercase() that accepts two file names, and copies all lines
that do not start with a lowercase letter from the first file to the second file.
27. Write a program that reads characters from the keyboard one by one. All lower case
characters get stored inside the file LOWER, all upper case characters get stored inside the file
UPPER and all other characters get stored inside file OTHERS.
28. Write a method in Python to write multiple lines of text contents into a text file
daynote.txt line.
29. Write a function in python to count the number lines in a text file ‘Country.txt’ which is
starting with an alphabet ‘W’ or ‘H’.
30. Write a user defined function countwords() to display the total number of words present in
the file from a text file “Quotes.Txt”.
31. Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT” and
count the number of times “AND” occurs in the file. (include AND/and/And in the counting)
32. Write a function DISPLAYWORDS( ) in python to display the count of words starting
with “t” or “T” in a text file ‘STORY.TXT’.
33. Write a function that counts and display the number of 5 letter words in a text file
“Sample.txt
34. Write a function to display those lines which start with the letter “G” from the text file
“MyNotes.txt”
35. Write a function in python to read lines from file “POEM.txt” and display all those words,
which has two characters in it.
36. Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to
count and display the occurrence of the word “Catholic” or “mother”.
37. Write a method/function COUNTLINES_ET() in python to read lines from a text file
REPORT.TXT, and COUNT those lines which are starting either with ‘E’ and starting with
‘T’ respectively. And display the Total count separately.
38. Write a method/function SHOW_TODO( ) in python to read contents from a text file
ABC.TXT and display those lines which have occurrence of the word ‘‘TO’’ or ‘‘DO’’.
39. Write a function in Python that counts the number of “Me” or “My” words present in a
text file “STORY.TXT”.
40. Write a function AMCount() in Python, which should read each character of a text file
STORY.TXT, should count and display the occurrences of alphabets A and M (including
small cases a and m too).
41. Write a function VowelCount() in Python, which should read each character of a text file
MY_TEXT_FILE.TXT, should count and display the occurrence of alphabets vowels.
42. Write a function filter(oldfile, newfile) that copies all the lines of a text file “source.txt”
onto “target.txt” except those lines which starts with “@” sign.
43. Consider a binary file Employee.dat containing details such as empno: ename: salary
(separator ':'). Write a Python function to display details of those employees who are earning
between 20000 and 40000 (both values inclusive).
44. Consider the file “SarojiniPoem.txt”
Like a joy on the heart of a sorrow,
The sunset hangs on a cloud;
A golden storm of glittering sheaves,
Of fair and frail and fluttering leaves,
The wild wind blows in a cloud.
Hark to a voice that is calling
To my heart in the voice of the wind:
My heart is weary and sad and alone,
For its dreams like the fluttering leaves have gone,
And why should I stay behind?
Based on the above file, answer the following question:
What would be the output of following code?

file = open ("sarojiniPoem.txt","r")


text = file.readlines()
file.close()
for line in text :
print (line , end = ' ' )
print ( )

(b) Modify the program so that the lines are printed in reverse order.
(c) Modify the code so as to output to another file instead of the screen. Let your script
overwrite the output file.
(d)Change the script of part (c) that it append output to an existing file.
(e) Modify the program so that each line is printed with a line number at beginning.
45. Following is the structure of each record in a data file named "PRODUCT.DAT".
("prod_code": value, "prod_desc": value, "stock": value)
The values for prod_code and prod_desc are strings and the value for stock is an integer.

46. Write a function in Python to update the file with a new value of stock. The stock and the
product_code, whose stock is to be updated, or to be inputted during the execution of the
function.
47. Write a program to read entire data from file data.csv.
48. Write a program to search the record from “data.csv” according to the admission number
input from the user. Structure of record saved in “data.csv” is ADM_no, Name, Class, Sec,
Marks.
49. Write a program to read all the content of “student.csv” and display records of only those
students who scored more than 80 marks. Records stored in studentsis in format: Rollno,
Name, Marks.
50. Write a program to display all the records from products.csv whose price is more than 300.
Format of record stored in product.csv is product id, product name, price.
51. Write a program to copy the data from “data.csv” to “temp.csv”.
52. Write a program to count the number of records present in “data.csv” file.
53. Arun, during Practical Examination of Computer Science, has been assigned an
incomplete search() function to search in a pickled file student.dat. The File student.dat is
created by his Teacher and the following information is known about the file. • File contains
details of students in [roll_no,name,marks] format.
• File contains details of 10 students (i.e. from roll_no 1 to 10) and separate list of each
student is written in the binary file using dump().
Arun has been assigned the task to complete the code and print details of roll number 1.
def search():
f = open("student.dat",____) #Statement-1
____: #Statement-2
while True:
rec = pickle.____ #Statement-3
if(____): #Statement-4
print(rec)
except:
pass
____ #Statement-5

1) In which mode Arun should open the file in Statement-1?


a) r b) r+ c) rb d) wb
2) Identify the suitable code to be used at blank space in line marked as Statement2
a) if(rec[0]==1) b) for i in range(10) c) try d) pass

3) Identify the function (with argument), to be used at blank space in line marked as
Statement-3.
a) load() b) load(student.dat) c) load(f) d) load(fin)

4) What will be the suitable code for blank space in line marked as Statement-4.
a) rec[0]==2 b) rec[1]==2 c) rec[2]==2 d) rec[0]==1

5) Which statement Arun should use at blank space in line marked as Statement4 to close the
file.
a) file.close() b) close(file) c) f.close() d) close()

54. Radha Shah is a programmer, who has recently been given a task to write a python code to
perform the following CSV file operations with the help of two user defined
functions/modules:
a. CSVOpen() : to create a CSV file called BOOKS.CSV in append mode containing
information of books – Title, Author and Price.
b. CSVRead() : to display the records from the CSV file called BOOKS.CSV where the field
title starts with 'R'.
She has succeeded in writing partial code and has missed out certain statements, so she has
left certain queries in comment lines.
import csv
def CSVOpen():
with open('books.csv','______',newline='') as csvf: #Statement-1
cw=______ #Statement-2
______ #Statement-3
cw.writerow(['Rapunzel','Jack',300])
cw.writerow(['Barbie','Doll',900])
cw.writerow(['Johnny','Jane',280])
def CSVRead():
try:
with open('books.csv','r') as csvf:
cr=______ #Statement-4
for r in cr:
if ______: #Statement-5
print(r)
except:
print('File Not Found')
CSVOpen()
CSVRead()
You as an expert of Python have to provide the missing statements and other related queries
based on the following code of Radha. Answer any four questions (out of five) from the below
mentioned questions.
1) Choose the appropriate mode in which the file is to be opened in append mode (Stmt 1)
a. w+ b. ab c. r+ d. a
2) Which statement will be used to create a csv writer object in Statement 2.
a. csv.writer(csvf) b. csv.writer(csvf) c. csvf.writer() d. cs.writer(csvf)
3) Choose the correct option for Statement 3 to write the names of the column headings in the
CSV file, BOOKS.CSV.
a. cw.writerow('Title','Author','Price') b. cw.writerow(['Title','Author','Price'])
c. cw.writerows('Title','Author','Price') d. cw.writerows(['Title','Author','Price'])
4) Which statement will be used to read a csv file in Statement 4.
a. cs.read(csvf) b. csv.reader(csvf) c. csvf.read() d. csvf.reader(cs)
5) Fill in the appropriate statement to check the field Title starting with ‘R’ for Statement 5 in
the above program.
a. r[0][0]=='R' b. r[1][0]=='R' c. r[0][1]=='R d. d. r[1][1]=='R'

55. Create a CSV file "Groceries" to store information of different items existing in a shop.
The information is to be stored w.r.t. each item code, name, price, qty. Write a program to
accept the data from user and store it permanently in CSV file.
56. Write a single loop to display all the contents of a text file “sports.txt” after removing
leading and trailing whitespaces.

FILL IN THE BLANKS:


1. A collection of bytes stored in computer’s secondary memory is known as _______.
2. ___________ is a process of storing data into files and allows to performs various tasks such as read,
write, append, search and modify in files.
3. The transfer of data from program to memory (RAM) to permanent storage device (hard disk) and vice
versa are known as __________.
4. A _______ is a file that stores data in a specific format on secondary storage devices.
5. In ___________ files each line terminates with EOL or ‘\n’ or carriage return, or ‘\r\n’.
6. To open file data.txt for reading, open function will be written as f = _______.
7. To open file data.txt for writing, open function will be written as f = ________.
8. In f=open(“data.txt”,”w”), f refers to ________.
9. To close file in a program _______ function is used.
10. A __________ function reads first 15 characters of file.
11. A _________ function reads most n bytes and returns the read bytes in the form of a string.
12. A _________ function reads all lines from the file.
13. A _______ function requires a string (File_Path) as parameter to write in the file.
14. A _____ function requires a sequence of lines, lists, tuples etc. to write data into file.
15. To add data into an existing file ________ mode is used.
16. A _________ function is used to write contents of buffer onto storage.
17. A text file stores data in _________ or _________ form.
18. A ___________ is plain text file which contains list of data in tabular form.

19. You can create a file using _________ function in python.


20. A __________ symbol is used to perform reading as well as writing on files in python.

MULTIPLE CHOICE QUESTIONS:

21. Every file has its own identity associated with it. Which is known as –
a. icon b. extension c. format d. file type

22. Which of the following is not a known file type?


a. .pdf b. jpg c. mp3 d. txp

23. In f=open(“data.txt”, “r”), r refers to __________.


a. File handle b. File object c. File Mode d. Buffer

24. EOL stands for


a. End Of Lineb. End Of List c. End of Lines d. End Of Location

25. Which of the following file types allows to store large data files in the computer memory?
a. Text Files b. Binary Filesc. CSV Files d. None of these

26. Which of the following file types can be opened with notepad as well as ms excel?
a. Text Files b. Binary Files c. CSV Files d. None of these

27. Which of the following is nor a proper file access mode?


a. close b. read c. write d. append

28. To read 4th line from text file, which of the following statement is true?
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

29. Which of the following function flushes the files implicitly?


a. flush() b. close() c. open() d. fflush()

30. Which of the following functions flushes the data before closing the file?
a. flush() b. close() c. open() d. fflush()
31. What is the output of the following code?

a) True b) False c) None d) Error

ANSWER THE FOLLOWING QUESTIONS:

32. Name two types of data files available in python .


33. In text file each line is terminated by a special character called _______
34. In python, default EOL character is _______
35. Is there any delimiter of line in binary File?
36. Processing of binary file is faster than text file (T/F)
37. Writing data is same as appending data in file. (T/F)
38. Can we read file without opening. (T/F)

39. Which function is used to open a file?


40. Write the code in python to open a file named “try.txt”
41. f = open(“data.txt”)
Identify name of file.
Identify name of function.
What is ‘f’ in above code?
The above statement will __________ file in ________ mode.
42. What is the default mode of opening a file?
43. Write a statement in python to open a file named “data.txt” stored in folder “content” in D drive.
44. What error is returned by following statement, if file “try.txt” does not exist?
f = open (“try.txt”)
45. Name the three modes in which file can be opened in python.
46. What is the purpose of ‘r’ as prefix in the given statement?
f = open(r “d:\color\flower.txt”)
47. What do you mean by file object in python?
48. Another name of file object is _______
49. Write the symbols used in text file mode for the following operations.
Read Only, Write only, Read and Write, Write and Read
50. Write the symbols used in binary file mode for the following operations.
Read Only, Write only, Read and Write, Write and Read
51. The difference between r+ and w+ mode?
52. The difference between write and append mode?
53. Which method is used to close the file in python?
54. The statement to close the file “test.txt” which is associated with file object named “fo”.
55. The difference between read() and read(n) functions?
56. The difference between readline() and readlines() ?
57. A program to read first 10 characters from a file named “data.txt”
f = open(“data.txt”,”r”)
data = f.read(10)
print(data)
58. A program to read entire content from the file named “test.txt”
f = open(“test.txt, “r”)
d = f.read()
print(d)

59. Write the output of the following, if the data stored in file “try.txt” is given below.
(“f” is the file object associated with file)
Try Try but never cry
print(f.read(4))
print(f.read(5))
print(f.read())

60. A program in python to read first line from the file(“data.txt”)

61. A program in python to display number of lines in a file(“data.txt”).


62. A program in python to display first line from the file(“data.txt”) using readlines().

63. What are the two built-in functions to read a line of text from standard input, which is by default the
keyboard?
A. Raw_input B. Input C. Read D. Scanner

64. What will be the output of the following code snippet?


txt = input("Enter your input: ")
print ("Received input is : ", txt)
A. Enter your input: Hello Viewers C. Both are correct
Received input is : Hello Viewers
B. Enter your input: [x*5 for x in range(2,10,2)] D. None of the above
Received input is : [10, 20, 30, 40]

65. readlines() method return _________


a. String b. List c. Dictionary d. Tuple

66. Which function is used to read data from Text File?


a. read( ) b. writelines( ) c. pickle( ) d. dump( )

67. Which of the following will read entire content of file(file object ‘f’)?
a. f.reads( ) b. f.read( ) c. f.read(all) d. f.read( * )

68. Which symbol is used for append mode?


a. ap b. a c. w d. app

69. Which of the following options can be used to read the first line of a text file data.txt?
a. f = open(‘data.txt’); f.read() b. f = open(‘data.txt’,’r’); f.read(n)
c. myfile = open(‘data.txt’); f.readline() d. f = open(‘data.txt’); f.readlines()

70. File in python is treated as sequence of ________________


a. Bytes b. Bites c. bits d. None of the above

71. Which function is used to write data in binary mode?


a. write b. writelines c. pickle d. dump

72. Which function is used to force transfer of data from buffer to file?
a. flush( ) b. save( ) c. move( ) d. None of the above

73. Let the file pointer is at the end of 3rd line in a text file named “data.txt”. Which of the following option
can be used to read all the remaining lines?
a. f.read( ) b. f.read(all) c. f.readline( ) d. f.readlines( )

74. ____________________ module is used for serializing and de-serializing any Python object structure.
a. pickle b. unpickle c. pandas d. math

75. Which of the following error is returned when we try to open a file in write mode which does not exist?
a. FileNotFoundError b. FileFoundError c. FileNotExistError d. None of the above

76. ______________ function returns the strings.


a. read( ) b. readline( ) c. Both of the above d. None of the above

77. The syntax of seek() is: file_object.seek(offset [, reference_point])What is reference_point indicate?


a. reference_point indicates the starting position of the file object
b. reference_point indicates the ending position of the file object
c. reference_point indicates the current position of the file object
d. None of the above.

78. Identify the invalid mode from the following.


a. a b. r+ c. ar+ d. w

79. Which of the following is an invalid mode of file opening?


a. read only mode b. write only mode c. read and write mode d. write and append mode

80. readlines( ) function returns all the words of the file in the form of List. (T/F)
a. True b. False

81. What is ‘f’ in the following statement?


f=open("Data.txt" , "r")
a. File Name b. File Handle c. Mode of file d. File Handling

82. What is full form of CSV


a. Comma Separation Value b. Comma Separated Value
c. Common Syntax Value d. Comma Separated Variable

83. Which statement will open file “data.txt” in append mode?


a. f = open(“data.txt” , “a”) b. f = Open(“data.txt” , “ab”)
c. f = new(“data.txt” , “a”) d. open(“data.txt” , “a”)

84. What error is returned by the following statement if the file does not exist?
f=open("A.txt")
a. FileNotFoundError b. NotFoundError
c. FileNotFound d. FoundError

85. Which statement will return error?


import pickle
f=open("data.dat",'rb')
d=pickle.load(f)
f.end()
a. Statement 1 b. Statement 2
c. Statement 3 d. Statement 4

86. Which of the following function takes two arguments?


a. load( ) b. dump( ) c. both of the above d. none of the above

87. Almost all the files in our computer stored as _______ File.
a. Text b. Binary c. CSV d. None of the above

88. .pdf and .doc are examples of __________ files.


a. Text b. Binary c. CSV d. None of the above

89. Fill in the blanks in the following code of writing data in binary files. Choose the answer for statement 1
import ___________ # Statement 1
rec = [ ]
while True:
rn = int(input("Enter"))
nm = input("Enter")
temp = [rn, nm]
rec.append(temp)
ch = input("Enter choice (Y/N)")
if ch.upper == "N":
break
f = open("stud.dat", "____________") #statement 2
__________ .dump(rec, f) #statement 3
_______.close( ) # statement 4

a. csv b. unpickle c. pickle d. load

90. Refer to the above code and choose the option for statement2.
a. w b. w+ c. wb d. write

91. Refer to the above code and choose the option for statement 3
a. unpickle b. write c. pickle d. None of the above

92. Refer to the above code and choose the option for statement 4.
a. f b. rec c. file d. stud

93. The syntax of seek() is:file_object.seek(offset [, reference_point] What is the default value of
reference_point
a. 0 b. 1 c. 2 d. 3

94. _______ function returns the current position of file pointer.


a. get( ) b. tell( ) c. cur( ) d. seek( )

95. Which statement will move file pointer 10 bytes backward from current position.
a. f.seek(-10, 0) b. f.seek(10, 0) c. f.seek(-10, 1) d. None of the above

96. When we open file in append mode the file pointer is at the _________ of the file.
a. end b. beginning c. anywhere in between the file d. second line of the file

97. When we open file in write mode the file pointer is at the _______ of the file.
a. end b. beginning c. anywhere in between the file d. second line of the file

98. Write the output of the First Print statements:


f=open("data.txt",'w')
f.write("Hello")
f.write("Welcome to my Blog")
f.close()
f=open("data.txt",'r')
d=f.read(5)
print(d) # First Print Statement
f.seek(10)
d=f.read(3)
print(d) # Second Print Statement
f.seek(13)
d=f.read(5)
print(d) # Third Print Statement
d=f.tell()
print(d) # Fourth Print Statement
a. Hello b. Hell c. ello d. None of the above

99. Refer to the above code: Write the output of Second Print Statement
a. om b. me c. co d. None of the above

100. Refer to the above code: Write the output of Third Print Statement
a. e to m b. e to my c. to my d. None of the above

101. Refer to the above code: Write the output of Fourth Print Statement
a. 17 b. 16 c. 19 d. 18

102. A _____________ is a named location on a secondary storage media where data are permanently stored
for later access.
a. file b. variable c. comment d. token

103. A _____ file consists of human readable characters.


a. Binary b. Text c. Both of the above d. None of the above

104. Which of the following file require specific programs to access its contents?
a. Binary b. Text c. CSV d. None of the above

105. Which of the following file can be opened in any text editor?
a. Binary b. Text c. Both of the above d. None of the above

106. Each line of a text file is terminated by a special character, called the ________
a. End of File b. End of Line c. End of Statement d. End of program

107. In which of the following data store permanently?


a. File b. Variable c. Both of the above d. None of the above
108. Identify the correct statement to open a file:
a. f = open(“D:\\myfolder\\naman.txt”) b. f = open(“D:\myfolder\naman.txt”)
c. f = open(“D:myfolder#naman.txt”) d. f = Open(“D:\myfolder\naman.txt”)

109. Which of the following are the attributes of file handle?


a. closed b. name c. mode d. All of the above

110. Write the output of the following:


>>> f = open("test.txt","w")
>>> f.closed
a. True b. False c. Yes d. No

111. Write the output of the following:


>>> f = open("test.txt","w")
>>> f.mode
a. ‘w’ b. ‘r’ c. ‘a’ d. ‘w+’

112. Write the output of the following:


>>> f = open("test.txt","w")
>>> f.name
a. ‘test’ b. ‘test.txt’ c. ‘test.dat’ d. file does not exist

113. Write the output of the following:


>>> f = open("test.txt","w")
>>> f.close()
>>> f.closed
a. True b. False c. Yes d. No

114. Which of the following attribute of file handle returns Boolean value?
a. name b. closed c. mode d. None of the above

115. Ravi opened a file in python using open( ) function but forgot to specify the mode. In which mode the
file will open?
a. write b. append c. read d. read and write both

116. Which of the following is invalid mode of opening file?


a. r b. rb c. +r d. None of the above

117. Which of the following mode will opens the file in read, write and binary mode?
a. ‘wb+ b. ‘+wb’ c. Both of the above d. None of the above

118. In the given statement, the file myfile.txt will open in _______________ mode.
myObject=open(“myfile.txt”, “a+”)
a. append and read b. append and write c. append and read and binaryd. All of the above

119. Ravi opened the file myfile.txt in append mode. In this file the file object/file handle will be at
the __________
a. beginning of the file b. end of the file c. second line of the file d. the end of the
first line

120. Ravi opened the file myfile.txt in write mode. In this file the file object/file handle will be at
the ______________
a. beginning of the file b. end of the file c. second line of the file d. the end of the
first line

121. Ravi opened the file myfile.txt in read mode. In this file the file object/file handle will be at
the _______________
a. beginning of the file b. end of the file c. second line of the file d. the end of the
first line

122. Ravi opened a file in a certain mode. After opening the file, he forgot the mode. One interesting fact
about that mode is ” If the file already exists, all the contents will be overwritten”. Help him to identify the
correct mode.
a. read mode b. write mode c. append mode d. binary and read mode

123. Ram opened a file in a certain mode. After opening the file, he forgot the mode. The interesting facts
about that mode are ” If the file doesn’t exist, then a new file will be created” and “After opening file in that
mode the file handle will be at the end of the file” Help him to identify the correct mode.
a. read mode b. write mode c. append mode d. binary and read mode

124. Which of the following function is used to close the file?


a. close( ) b. end( ) c. quit( ) d. exit( )

125. open( ) function returns a file object called ______________


a. object handle b. file handle c. read handle d. write handle

126. Which of the following is the valid way to open the file?
a. with open (file_name, access_mode) as fo: b. fo = open (file_name, access_mode)
c. Both of the above d. None of the above

127. Rohan opened the file “myfile.txt” by using the following syntax. His friend told him few advantages of
the given syntax. Help him to identify the correct advantage.
with open ("myfile.txt", "a") as file_object:
a. In case the user forgets to close the file explicitly the file will closed automatically.
b. file handle will always be present in the beginning of the file even in append mode.
c. File will be processed faster d. None of the above

128. Mohan wants to open the file to add some more content in the already existing file. Suggest him the
suitable mode to open the file.
a. read mode b. append mode c. write mode d. All of the above

129. Aman jotted down few features of the “write mode”. Help him to identify the valid features.
a. If we open an already existing file in write mode, the previous data will be erased
b. In write mode, the file object will be positioned at the beginning of the file.
c. In write mode, if the file does not exist then the new file will be created.
d. All of the above

130. Ananya jotted down few features of the “append mode”. Help her to identify the valid features.
a. If we open an existing file in append mode, the previous data will remain there.
b. In append mode the file object will be positioned at the end of the file.
c. In append mode, if the file does not exist then the new file will be created.
d. All of the above

131. Which of the following methods can be used to write data in the file?
a. write( ) b. writelines( ) c. Both of the above d. None of the above

132. Write the output of the following:


>>> f = open("test.txt","w")
>>> f.write("File\n#Handling")
a. 17 b. 16 c. 14 d. 15

133. Which of the following error is returned by the given code:


>>> f = open("test.txt","w")
>>> f.write(345)
a. Syntax Error b. TypeError c. StringError d. Run Time Error

134. Which of the following method is used to clear the buffer?


a. clear( ) b. buffer( ) c. flush( ) d. clean( )

135. Which of the following method does not return the number of characters written in the file.
a. write( ) b. writelines( ) c. Both of the above d. None of the above

136. Fill in the blank in the given code:


>>> fo = open("myfile.txt",'w')
>>> lines = ["Hello \n", "Writing strings\n", "third line"]
>>> fo._____________(lines)
>>>myobject.close()
a. write( ) b. writelines( ) c. writeline( ) d. None of the above

137. In order to read the content from the file, we can open file in ___________________
a. “r” mode b. “r+” mode c. “w+” mode d. All of the above

138. Which of the following method is used to read a specified number of bytes of data from a data file.
a. read( ) b. read(n) c. readlines( ) d. reading(n)

139. Write the output of the following:


f=open("test.txt","w+")
f.write("File-Handling")
a=f.read(5)
print(a)

140. Write the output of the following:


f=open("test.txt","w+")
f.write("File-Handling")
f.seek(0)
a=f.read(5)
print(a)

141. Write the output of the following:


f=open("test.txt","w+")
f.write("FileHandling")
f.seek(5)
a=f.read(5)
print(a)

142. Write the output of the following:


f=open("test.txt","w+")
f.write("FileHandling")
f.seek(0)
a=f.read()
print(a)

143. Write the output of the following:


f=open("test.txt","w+")
f.write("FileHandling")
f.seek(0)
a=f.read(-1)
print(a)

144. Which method reads one complete line from a file?

145. Write the output of the following:


f=open("test.txt","w+")
f.write("File\nHandling")
f.seek(0)
a=f.readline(-1)
print(a)

146. Write the output of the following:


f=open("test.txt","w+")
f.write("File\nHandling")
f.seek(0)
a=f.readline(2)
print(a)

147. Select the correct statement about the code given below:
>>> myobj=open("myfile.txt", 'r')
>>> print(myobj.readlines())
a. This code will read one line from the file. b. This code will read all the lines from the file.
c. This code will read only last line from the file. d. None of the above

148. Write the output of the following:


f=open("test.txt","w+")
L = ["My name\n", "is\n", "Amit"]
f.writelines(L)
f.seek(0)
a=f.readlines()
print(type(a))
a. <class ‘tuple’> b. <class ‘list’> c. <class ‘string’> d. None of the
above

149. Which of the following function return the data of the file in the form of list?
a. read( ) b. read(n) c. readline( ) d. readlines( )

150. Sanjeev has written a program which is showing an error. As a friend of Sanjeev, help him to identify the
wrong statement.
f=open("test.txt","w") #Statement1
L = ["My name\n", "is\n", "Amit"] #Statement2
f.writeline(L) #Statement3
f.close() #Statement4
a. Statement1 b. Statement2 c. Statement3 d. Statement4

151. ____________________ function returns an integer that specifies the current position of the file object in
the file.
a. seek( ) b. tell( ) c. disp( ) d. None of the above

152. _______ method is used to position the file object at a particular position in a file.
a. tell( ) b. seek( ) c. put( ) d. None of the above

153. In reference to the code given below, the file object will move _______ bytes.
file_object.seek(10, 0)
a. 0 b. 10 c. 5 d. 4

154. Ravi wants to move the file object 5 bytes from the current position of the file object. As a friend of
Ravi, help him to write the code.
a. file_object.seek(5, 1) b. file_object.seek(1, 5)
c. file_object.seek(5, 0) d. file_object.seek(5, 2)

155. Write the output of the following code:


f=open("test.txt","w+")
f.write("My name is Amit\n")
f.seek(5,0)
a=f.read(5)
print(a)

156. Write the output of the following:


f=open("test.txt","r")
print(f.tell())

157. Write the output of the following:


f=open("test.txt","r")
print(f.tell(),end="6")
f.seek(5)
print(f.tell())

158. How many functions are used in the given code?


fileobject=open("practice.txt","r")
str = fileobject.readline()
while str:
print(str)
str=fileobject.readline()
fileobject.close()
a. 3 b. 4 c. 5 d. 6

159. _________ method is used to write the objects in a binary file.

160. _________ method is used to read data from a binary file.

162. Which module is to be imported for working in binary file?

163. Which of the following statement open the file “marker.txt” so that we can read existing content from
file?
a. f = open(“marker.txt”, “r”) b. f = Open(“marker.txt”, “r”)
c. f = open(“marker.txt”, “w”) d. None of the above

164. Which of the following statement open the file “marker.txt” as a blank file?
a. f = open(“marker.txt”, “r”) b. f = open(“marker.txt”, “rb”)
c. f = open(“marker.txt”, “w”) d. None of the above

165. Amit has written the following statement. He is working with ______
f = open("data", "rb")
a. Text File b. CSV File c. Binary File d. None of the above

166. Which of the following option is correct?


a. if we try to write in a text file that does not exist, an error occurs
b. if we try to read a text file that does not exist, the file gets created.
c. if we try to write on a text file that does not exist, the file gets Created.
d. None of the value

167. Correct syntax of tell( ) function is _________. #f is file object


a. f.tell( ) b. tell(f) c. f_tell( ) d. None of the above

168. The syntax of seek() is: file_object.seek(offset [, reference_point]). ___________________ value of


reference_point indicate end of file
a. 0 b. 1 c. 2 d. 3

169. Which of the following statement is wrong in reference to Text file?


a. A text file is usually considered as a sequence of characters consisting of alphabets, numbers and other
special symbols.
b. Each line of a text file is terminated by a special character.
c. Text files are not in Human readable form. d. Text files can be opened in Text editor.

170. Identify the statement which can not be interpret from the given code:
f = open("test.dat", "ab+")
a. test.dat is a binary file b. reading operation can be done on test.dat
c. appending operation can be done on test.dat d. test.dat contains records about books

171. Which module is to be imported for CSV file?


a. pickle b. unpickle c. csv d. pandas

172. Write the output of the following code :


import csv
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
print(row)
a. It prints the memory address where csv.reader object is stored b. It prints the first record of
data.csv
c. It prints all the records of data.csv d. None of the above

173. _________ function help us to read the csv file.


a. reader( ) b. read( ) c. read(n) d. readline( )

174. Write the output of the second print statement of the given code:
f=open("test.txt","r")
print(f.read())
f.seek(7)
print(f.read())
Output of first print statement is : MynameisAmit
a. isAmit b. sAmit c. Amit d. eisAmit

175. Which of the following method returns an integer value?


a. seek( ) b. read( ) c. tell( ) d. readline( )

176. ___________ refers to the process of converting the structure to a byte stream before writing it to the
file.
a. pickling b. unpickling c. reading d. writing

177. Fill in the blank in the given code :


import pickle
f = open("data.dat", "rb")
l = pickle._______(f)
print(l)
f.close()
a. dump b. load c. write d. list

178. Write the output of the following code:


f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.read()))
a. 13 b. 14 c. 15 d. 16

179. Write the output of the following code:


f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.readlines()))
a. 4 b. 5 c. 6 d. 7

180. Write the output of the following code:


f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.readline()))
a. 2 b. 3 c. 4 d. 5

181. Fill in the blank in the given code :


import pickle
f = open("data.dat", "wb")
L = [1, 2, 3]
pickle._______
f.close()
a. dump(f, L) b. load(f, L) c. dump(L, f) d. load(L, f)

182. Which of the following statement will return attribute error?


a. print(len(f.readlines( ).split( ))) #f is file handle b. print(len(f.readline( ).split( ))) #f is file handle
c. print(len(f.read( ).split( ))) #f is file handle d. None of the above

183. Ravi is writing a program for counting the number of words in a text file named “data.txt”. He has
written the incomplete code. Help him to complete the code.
f = open("_________","r") # Statement 1
d = f._________ # Statement 2
nw = d._________ # Statement 3
print("Number of words are", _________(nw)) # Statement 4

184. Identify the suitable code for blank space in the line marked as Statement 1
a. “data.txt” b. data.txt c. “data” d. data

185. Identify the suitable code for blank space in the line marked as Statement 2(Refer Q. 137)
a. readlines( ) b. readline( ) c. read( ) d. None of the above

186. Identify the suitable code for blank space in the line marked as Statement 3(Refer Q. 137)
a. split( ) b. break( ) c. words( ) d. jump( )

187. Identify the suitable code for blank space in the line marked as Statement 4(Refer Q. 137)
a. length b. len c. Len d. Length

188. Ananya was writing a program of reading data from csv file named “data.csv”. She writes the incomplete
code. As a friend of Ananya help her to complete the code.
________ csv #Statement1
f=open("data.csv", '_______') #Statement2
d=csv.___________(f) #Statement3
for __________ in d: #Statement4
print(row)
Identify the suitable option for Statement 1
a. import b. create c. data d. Import

189. Identify the correct option for Statement2 (Refer Q 141)


a. w b. a c. rb d. r

190. Identify the correct option for Statement3 (Refer Q 141)


a. readlines b. readline c. read d. reader

191. Identify the correct option for Statement4 (Refer Q 141)


a. row b. d c. csv d. i

192. Parth is writing a program to add/insert records in file “data.csv”. He has written the following code. As
a programmer, help him to execute it successfully.

import csv
field = [“Roll no” , “Name” , “Class”]
f = open(“_________” , ‘w’) #Statement1
d=__________.writer(f) #Statement2
d.writerow(field)
ch=’y’
while ch==’y’ or ch==’Y’:
rn=int(input(“Enter Roll number: “))
nm = input(“Enter name: “)
cls = input(“Enter Class: “)
rec=[_________] #Statement3
d.writerow(rec)
ch=input(“Enter more record??(Y/N)”)
f.close()

Identify the correct statement for Statement 1


a. data b. data.csv c. file_data.csv d. None of the above

193. Identify the correct statement for Statement 2. (Refer Q. 145)


a. CSV b. csv c. file d. csv_

194. Identify the correct statement for Statement 3. (Refer Q. 145)


a. rollno, name, class b. Roll no, Name, Class c. rn, nm, cls d. field

195. Chinki is writing a program to copy the data from “data.csv” to “temp.csv”. However she is getting some
error in executing the following program due to some missing commands. Help her to execute it successfully.

import csv
f=open(“data.csv”,”r”)
f1=open(“temp.csv”,’__________’) #Statement 1
d=csv._________(f) #Statement 2
d1=csv.writer(f1)
for i in d:
___________.writerow(i) #Statement3
f.close( )
f1.close( )

Identify the correct statement for Statement 1.


a. r b. rb+ c. wa d. w

196. Identify the correct statement for Statement 2.(Refer Q148.)


a. read b. reader( ) c. reader d. read(r)

197. Identify the correct statement for Statement 2.(Refer Q148.)


a. d b. d1 c. f d. f1

198. Simran is writing a function by name “bookshop” in which she is asking about book name and price
from the user and storing the details in a file “book.txt”. Function is showing some error due to some missing
words/commands. As a programmer help her to write the correct program.

_________________ bookshop: #Statement 1


b_name = input("Enter book name:")
b_price=int(input("Enter book price:"))
data = _________________([b_name, b_price]) #Statement 2
f = open("book.txt","w")
f.write(_____________) #Statement 3
f.close()

Identify the correct option for Statement 1.


a. def b. Def c. define d. Define

199. Identify the correct option for Statement 2.


a. int b. str c. char d. float

200. Identify the correct option for Statement 3.


a. data b. b_name c. b_price d. f

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy