Conlib Filehandlingtest1answerkey 20250305094905-1
Conlib Filehandlingtest1answerkey 20250305094905-1
MM :295
1 The modes of file in python are (a) r (b) r+(c) w (d) All of the these 1
ANS: (d)
ANS: (b)
ANS: (a)
4 To read the content of the file as a string via a file object "f" (a) f.read(2) (b) f.read()(c) f=file.readline() (d)
1
f.readlines()
ANS: (b)
ANS: (c)
ANS: (a)
ANS: (a)
ANS: (a)
ANS: (a)
ANS: (a)
ANS: (a)
12 Which of the following statement is not true regarding the opening modes of a file?
(a) When you open a file for reading, if the file does not exist, an error occurs.
(b) When you open a file for writing, if the file does not exist, an error occurs.
1
(c) When you open a file for reading, if the file does not exist, the program will open an empty file.
(d) When you open a file for writing, if the file does not exist, a new file is created.
(e) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
ANS: (c)
13 Which of the following commands can be used to read "n" number of characters from a file using the file object
1
<file>? (a) File.read(n) (b) N = file.read()(c) File.readline(n) (d) File.readlines()
ANS: (a)
14 Which of the following commands can be used to read the entire contents of a file as a string using the file object
1
<File>? (a) File.read(n) (b) File.read()(c) File.readline() (d) File.readlines()
ANS: (b)
15 Which of the following commands can be used to read the next line in a file using the file object <File>? (a)
1
File.read(n) (b) File.read()(c) File.readline() (d) File.readlines()
ANS: (c)
16 What does the <readlines()> method returns? (a) Str(b) A list of lines(c) List of single characters(d) List of integers 1
ANS: (b)
17 Which of the following command is used to open a file "d:temp.txt" for writing in binary format only? (a) fout = 1
open("d:story.txt", "wb") (b) fout = open("d:temp.txt", "wb") (c) fout = open(file = "d:temp.txt", "wb+") (d) fout =
open(file = "d:temp.txt", "wb+")
ANS: (b)
18 Which of the following functions do you use to write data in the binary format? (a) Write (b) Output(c) Dump (d)
1
Send
ANS: (c)
19 Which of the following are the attributes related to a file object? (a) Closed (b) Mode (c) Filename (d) Rename 1
20 What will be the output of the following code snippet? If we assume that file contains the given text:
ANS: (b)
22 Write a statement in Python to open a text file STORY.TXT, so that the new contents can be added at the end of it. 1
ANS: (a) It will open the file in append mode and data will be added at the end of the file only.
(b) It will open the file in write mode if file already exists then previous data will be erased.
ANS: (a) It opens the file in reading mode only in text format.
(b) It opens the file in writing mode only in text format. If the file exists, then it erases the previous data.
ANS: Tell(). It returns the current position of the file object (Integer value).
Seek(). It position the file object at the specified location.
ANS:
28
ANS: Comma Separated Values. It is a simple file format which store into the tabular format. This format file can
be open in notepad or spreadsheet. It works better as its takes less memory space and faster in data manipulation.
ANS:
ANS: The absolute path is the full path from we want to open/create the file.
The relative path is the path to some file with respect to your current working directory.
33 Nancy intends to position the file pointer to the beginning of a text file. Write Python statement for the same
1
assuming F is the Fileobject.
ANS: F.seek(0)
34 Write a statement in Python to open a text file STORY.TXT so that new contents can be added at the end of it. 1
ANS:
ANS:
ANS: Difference
37
1
Observe the following code and answer the questions that follow: (i) What type
(Text/Binary) of file is Mydata?
(ii) Fill the Blank 1 with statement to write “ABC” in the file “Mydata”.
38
ANS: 22
39 Assume that file “tear.txt”, already contain “I love my India” in the file. What will be the output of the given program:
ANS: 28
40 Assume that file “tear.txt”, already contain “I love my India” in the file. What will be the output of the given program: 1
ANS: 13
ANS: ife y
file_object.seek(offset[,reference_point]) 1
ANS: (a)
43 Which of the following statement is not correct? [CBSE 2021] (a) We can write 1
content into a text file opened using 'w' mode (b) We can write content into a text file opened using 'w+' mode
(c) We can write content into a text file opened using 'r' mode (d) We can write content into a text file
opened using 'r+' mode
ANS: (c)
44 Which of the following option is the correct Python statement to real and display the first 10 characters of a text file
"Notes.txt"?[CBSE 2021]
ANS: (c)
45 Which of the following is not a correct Python statement to open a text file "Notes.text" to write content into
it?[CBSE 2021]
1
(a) F=open('Notes.txt','w')
(b) F=open('Notes.txt','a') (c) F=open('Notes.txt','A') (d) F=open('Notes.txt','w+')
ANS: (c)
MyFile=open('Notes.txt')
1
Which of the following is the correct Python statement to close it?[CBSE 2021] (a)
MyFile=close('Notes.txt') (b) MyFile.close('Notes.txt') (c)cose.MyFile() (d) MyFile.close()
ANS: (d)
47 Which of the following option is the correct usage for the tell() of a file object?[CBSE 2021]
ANS: (b)
48 Which of the following statement is icorrect in the context of picked binary files?
(a) csv module is used for reading and writing objects in binary files.
1
(b) pickle module is used for reading and writing objects in binary files. (c) load() of the pickle module is used
to read objects. (d) dump() of the pickle module is used to read objects.
ANS: (a)
49 What is the significance of the seek() method? [CBSE 2021] (a) It seeks the absolute path of the file.
(b) It tells the current byte position of the file pointer within the file. (c) It places the file pointer at a desired 1
offset within the file. (d) It seeks the entire content of the file.
ANS: (c)
50 If the following statement is used to read the contents of a textfile object F:[CBSE 2021]
x=F.readlines() 1
Which of the following is the correct data type of x? (a) string (b) list (c) tuple (d) dictionary
ANS: (b)
What will be the output of the following Python code? [CBSE 2021] F=open (Notes.txt") 1
F.seek(29) S=F.read() print(S) (a) The way to get started is to (b) quit talking and
begin doing (c) The way to get started is to quit talking and begin doing (d) gniod nigeb dna gniklat tiuq ot si
detrats teg ot yaw eht
ANS: (b)
52 Which of the following Python modules is imported to store and retrieve objects using the process of serialization
and deserializaton?[CBSE 2021]
1
(a) csv
ANS: (a)
53 Which of the following function is used with the csv module in Python to read the contents of a csv file into an
object?
1
(a) readrow()
ANS: (c)
ANS: (d)
ANS: (c)
ANS: (d)
57 What is a CSV file? (a) A file that contains human-readable text (b) A file that contains binary data (c) A file
1
that contains comma-separated values (d) None of the above
ANS: (c)
(a) A path that specifies the location of a file relative to the root directory
1
(b) A path that specifies the location of a file relative to the current directory (c) A path that specifies the
location of a file using the full file path (d) None of the above
ANS: (b)
(a) A path that specifies the location of a file relative to the root directory
1
(b) A path that specifies the location of a file relative to the current directory (c) A path that specifies the
location of a file using the full file path (d) None of the above
ANS: (c)
ANS: (a)
62 What is the file mode 'a' in Python? (a) Read mode (b) Write mode (c) Append mode (d) None of the
1
above
ANS: (c)
ANS: (a)
64 Which of the following is NOT a type of file in Python? (a) Text file (b) Binary file (c) CSV file (d) JSON file 1
ANS: (d)
65 What is a text file? (a) A file that contains human-readable text (b) A file that contains binary data (c)
1
A file that contains comma-separated values (d) None of the above
ANS: (a)
(b) A file that contains binary data (c) A file that contains comma-separated values (d) None of the above
ANS: (b)
67 Which of the following modes is used to open a text file for reading?
(a) 'r' 1
ANS: (a)
68 Which of the following modes is used to open a text file for both reading and writing?
(a) 'r' 1
(b) 'w' (c) 'a' (d) 'r+'
ANS: (a)
69 What is the purpose of the with statement when working with files?
(a) To open the file in read mode
1
(b) To open the file in write mode (c) To ensure that the file is closed properly after use (d) To manipulate the
data in the file
ANS: (c)
ANS: (a)
ANS: (b)
ANS: (c)
(a) reader() 1
(b) writer() (c) load() (d) read()
ANS: (c)
74 Which of the following statement is icorrect in the context of picked binary files?[CBSE 2021] 1
(a) csv module is used for reading and writing objects in binary files.
(b) pickle module is used for reading and writing objects in binary files. (c) load() of the pickle module is used
to read objects. (d) dump() of the pickle module is used to read objects.
ANS: (a)
75 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): In Python, a text file can only be opened in read mode or write mode, but not both at the same time.
Reason (R): (a) The read mode allows the user to read the content of the file. (b) The write mode 1
allows the user to modify or overwrite the content of the 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 and 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
ANS: (a)
76 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): In Python, a text file can only be opened in read mode or write mode, but not both at the same time.
Reason (R): (a) This is a recommended practice to ensure that the file is closed even if an exception occurs. 1
(b) The with clause can also reduce the amount of boilerplate code needed to open and close the 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 and 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
ANS: (a)
77 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the
correct answer out of the following choices. Assertion (A): In Python, a text file can only be opened in read mode or
write mode, but not both at the same time. Reason (R): (a) The write() method appends the string to the end of the
file. (b) The write() method returns the number of characters written to the file. (a) Both Assertion (A) 1
and Reason (R) are true and Reason (R) is the correct explanation of Assertion (A) (b) Both Assertion (A) and
Reason (R) are true and 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
ANS: (a)
78 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices. Assertion (A): In Python, the read() method can be used to read the entire
content of a text file as a single string. Reason (R): (a) The read() method reads the file from the current position of
the file pointer. (b) The read() method raises an IOError exception if the file is not readable. 1
(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 and 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
ANS: (a)
79 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices. Assertion (A): Opening a text file using 'w' mode deletes the existing content of
the file. Reason (R): 'w' mode opens the file for writing and truncates the file to zero length, which means all the
existing content of the file will be deleted. (a) Both Assertion (A) and Reason (R) are true and Reason (R) is the 1
correct explanation of Assertion (A) (b) Both Assertion (A) and Reason (R) are true and 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
ANS: (a)
80 Which method is used to write multiple lines of text to a file? (a) write() (b) writelines() (c) read() (d)
1
readline()
ANS: (b)
(a) write() 1
ANS: (c)
(a) write() 1
(b) writelines() (c) read() (d) readline()
ANS: (d)
83 Which method is used to read all the lines of text from a file into a list?
ANS: (d)
84 Which method is used to move the file pointer to a specific position in the file? 1
(a) seek()
(b) tell() (c) read() (d) readline()
ANS: (a)
(a) 'r' 1
ANS: (b)
ANS: (c)
(a) write() 1
ANS: (b)
ANS: (b)
ANS: (b)
import pickle
1
data = [1, 2, 3, 4, 5] with open("example.pkl", "wb") as f: pickle.dump(data, f) with
open("example.pkl", "rb") as f: loaded_data = pickle.load(f) print(loaded_data) (a) [1, 2, 3, 4, 5] (b)
example.pkl (c) None of the above (d) An error will occur
ANS: (a)
ANS: (d)
92 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
1
answer out of the following choices.
Assertion (A): The 'readline()' method reads a single line of text from a file.
Reason (R): The 'readline()' method reads the specified number of characters from the current position in the file, or
if no size is specified, it reads a single line of text from the file and returns it as a string. (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 and 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
ANS: (c)
93 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): The 'readlines()' method reads all the lines of text from a file and returns them as a list of strings.
Reason (R): The 'readlines()' method reads the specified number of lines from the file, or if no size is specified, it 1
reads all the lines of text from the file and returns them as a list of strings. (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
and 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
ANS: (b) Both A and R are true and R is not the correct explanation for A
94 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): The 'seek()' method is used to move the file pointer to a specified position in the file.
1
Reason (R): The 'seek()' method is used to move the file pointer to the end of the file, which is useful for appending
data to the end of the 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 and 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
95 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): The 'tell()' method returns the current position of the file pointer in the file.
1
Reason (R): The 'tell()' method returns the size of the file in bytes. (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 and
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
96 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices.
Assertion (A): The 'writelines()' method writes a list of strings to a file, with each string representing a line of text.
1
Reason (R): The 'writelines()' method writes a single string to a file, with each newline character ('n') representing a
new line of text. (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 and 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
97 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
1
answer out of the following choices.
Assertion (A): Binary files can only be opened in read mode.
Reason (R): Binary files are not writable, and can only be read from. (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
and 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
98 In the following questions, a statement of assertion (A) is followed by a statement of reason (R). Choose the correct
answer out of the following choices. Assertion (A): The dump() method in the pickle module is used to write data to
a binary file. Reason (R): The dump() method serializes Python objects into a binary format that can be written to a
1
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 and 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
ANS: (a) Both A and R are true and R is the correct explanation for A.
99 Mr. Iqbal is a programmer, who has recently joined the company and given a task to write a python code to perform
the following binary file operations with the help of two user defined functions/modules:
(a) Add() to create a binary file called school.dat containing student information – roll number, name,age
and marks of English, Maths and Science(out of 100) of each student.
(b) Display() to display the information of all students. He has succeeded in writing partial code 5
and has missed out certain statements, so he has left certain queries in comment lines. You as an expert of Python
have to provide the missing statements. import ____________ # statement 1 to
import module/package def add(): st={} file =open('school.dat','____')
# Statement 2 to append the record in binary file. ch='Y' while ch=='Y' or ch=='y':
admno=int(input("enter the admission number:")) name=input("enter
the name: ") age=int(input("enter the age: ")) engmarks=int(input("enter
english marks: ")) scienmarks=int(input("enter science marks: "))
mathmarks=int(input("enter maths marks: ")) st['admno']=admno
st['name']=name st['age']=age st['english']=engmarks
st['maths']=mathmarks st['science']=scienmarks
pickle.dump(st,________) # statement 3 to specify object
ch=input("do you wish to continue: ") file.close() def display(): st={}
file=open('school.dat','rb') try: while True: st=pickle._________(file)
# statement 4 to store data in st. print(st) except EOFError: print("error")
file._______ # statement 5 to close file object. (a) Write the statement to import module.
(b) Write the statement to append the record in binary. (c) Write the statement to specify object.
(d) Write the statement to store data in st. (e) Statement to close the five object.
(e) close()
100 Nisha, an intern in ABC Pvt. Ltd., is developing a project using the csv module in Python. She has partially
developed the code as follows leaving out statements about which she is not very confident. The code also contains
errors in certain statements. Help her in completing the code to read the desired CSV File named "Employee.csv"
ANS: (a) Csv (b) "Employes.csv","x" (c) reader(File) (d) for R is ER:
(e) R[2]
101 Mr. Mathew is a programmer, who has recently joined the company and given a task to write a python code to
perform update operation in binary file (stud.dat) which contain admno (Admission No) and fee(Fee paid by
student).
He has succeeded in writing partial code and has missed out certain statements, You as an expert of
Python have to provide the missing statements.
(c) file.tell()
102 Write a python program to accept a line from the user and store that in a file “story.txt”. 2
ANS:
103 Write a python program to accept the multiple lines from the user and store is into the file. 2
ANS:
104 Write the python program using writelines() function to store the following data:
2
[Filename: ch4/ch-4_Q10]
ANS:
105 Write a program to read and display the content of the "multiline.txt". [Filename: ch4/ch-4_Q11] 2
ANS:
106 Write a program to read the content of the file line by line and display. Assuming that file name is “multiline.txt”. 2
ANS:
107 Write a program to Read the content of the file line by line and print without ‘n’. Assuming that file name is
2
“multiline.txt”.
ANS:
108 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:
2
"This is my website. I have displayed my preferences in the CHOICE section."
The countmy( ) function should display the output as:
"my occurs 2 times".
ANS:
109 Write a method in Python to read lines from a text file INDIA.TXT, to find and display the occurrence of the word
"India".
ANS:
110 Write a function to count the “me” and “my” in a file “India.txt”. 2
ANS:
111 Write a function to count the Upper case vowel in file “All.txt”. 2
ANS:
112 Write a function to copy all the upper case words to another file. 2
ANS:
113 Write a function to display the alternate lines from the file. 2
ANS:
114 Write a function to read the content from the file and display each word in reverse order. 2
ANS:
115 Write a function to read the content from the file and print all the upper case word. 2
ANS:
116 Write a function to read the content from the file “Story.txt”. The function will take a string as parameter
2
and search the string into the file.
ANS:
117 Write the program to count the number of characters in the file “India.txt”. 3
ANS:
118 Write the program using function to count the lower case vowel in the file “India.txt”. 3
ANS:
ANS:
120 Write a function to read the content from the "quotes.txt" and transfer the content to the file "Duplicate.txt" but while
transfer all the upper case character to lower case character, lower case character to uppercase character and rest 3
of the character as it is.
ANS:
ANS:
ANS:
ANS:
125 Define a function remove_newline() which will remove new line from sample.txt 3
ANS:
126 Write a function read the content and transfer all the word which start with lower case letter to another file. 3
ANS:
127 Write a method in Python to read lines from a text file MYNOTES.TXT, and display those lines, which are
3
starting with the alphabet ―K.
ANS:
128 Write the function to read the content from the file display the word which are having the length of exactly 4
3
characters.
ANS:
129 Write a function addrecord() a add new record to the binary file “employee” using list. The list should consist
3
of employee number, employee name and salary.
ANS:
130 Write a function readall() the record from the binary file “employee” and display all the records. 3
ANS:
131 Write a function search () to search a record in a binary file according to the employee number entered by the
3
user. Also display the message “Record not found” in case record not found in the file.
ANS:
132 Write a function update () to update the record in the binary file “employee”, which consist of employee number,
3
employee name and salary. The updation should be done on the basis of employee number entered by the user.
ANS:
133 Write a function delrecord() to delete the record form the binary file “employee”. The record should be delete
3
on the basis of employee number.
ANS:
134 Write a function to read the content from the file “India.txt”, and store the frequency of each word in dictionary and
display the dictionary in the screen. 4
Content of the file is:
‘‘India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is capable of reaching.’’
Output of the file:
{‘India’: 4, ‘is’: 4, ‘the’: 4, ‘fastest’: 1, ‘growing’: 1, ‘economy.’: 1, ‘looking’: 2, ‘for’: 1, ‘more’: 1, ‘investments’: 1,
‘around’: 1, ‘globe.’: 1, ‘The’: 1, ‘whole’: 1, ‘world’: 1, ‘at’: 1, ‘as’: 1, ‘a’: 1, ‘great’: 1, ‘market.’: 1, ‘Most’: 1, ‘of’: 2,
‘Indians’: 1, ‘can’: 1, ‘foresee’: 1, ‘heights’: 1, ‘that’: 1, ‘capable’: 1, ‘reaching.’: 1}
ANS:
135 Write a function to read the content from the file “India.txt”, and store the frequency of each character in dictionary
and display the dictionary in the screen.
Content of the file is:
‘‘India is the fastest growing economy. 4
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is capable of reaching.’’
Output is:
{'I': 5, 'n': 15, 'd': 7, 'i': 15, 'a': 17, ' ': 39, 's': 13, 't': 15, 'h': 10, 'e': 20, 'f': 5, 'g': 8, 'r': 9, 'o': 17, 'w': 3, 'c': 4, 'm': 4, 'y': 1,
'.': 4, 'n': 4, 'l': 6, 'k': 3, 'v': 1, 'u': 1, 'b': 2, 'T': 1, 'M': 1, 'p': 1}
ANS:
136 Write a function addrecord() a add new record to the binary file “student” using list. The list should consist of student
4
number, student name and marks of the student.
ANS:
137 Write a function readall() the record from the binary file “student” and display all the records. 4
ANS:
138 Write a function search () to search a record in a binary file according to the student number entered by the user.
4
Also display the message “Record not found” in case record not found in the file.
ANS:
139 Write a function update () to update the record in the binary file “student”, which consist of student number, student
4
name and marks. The updation should be done on the basis of student number entered by the user.
ANS:
140 Write a function delrecord() to delete the record form the binary file “student”. The record should be deleted on the
4
basis of student number.
ANS:
141 Write a menu driven program to do the following option on binary file “Student”. The program should implement
through list which contains student number, student name and marks.
Student Menu
------------------
1) Add New Student
4
2) Display student details
3) Search student
4) Update a Record
5) Delete a Record
6) Save & Quit
ANS:
142 Write a program to count no. of records in csv file and display the records in tabular form.
1102 Swati 90 85 95 94 86
1103 Sam 90 80 94 96 80
1104 Imran 75 76 64 85 70
1105 Heena 85 80 80
ANS:
143 Write a function AverageColumn() which accepts csv file as parameter. It will print sum and average of each row.
For example if marks.csv contain
50,60,70,80,90
70,60,50,60,80 4
50,60,70,80,90
Then OUTPUT will be
Sum is : 350.0 Average is : 116.66666666666667
Sum is : 320.0 Average is : 106.66666666666667
Sum is : 350.0 Average is : 175.0
ANS:
144 Krishna is confused in Python code given below. Please help him to answer the following questions. 5
(a) Which line in the above code check for capital letter? (b) Which
line in the above code read the file “story. txt”? (c) Which line in the above code does not affect the execution of
program? (d) Which line is the above code coverts capital letter to small letter? (e) Which line is the above code
opens the file in write mode? (f) Which line is the above code saves the data? (g) Which line(s) is/are the part of
selection statement. (h) Which line(s) is/are used to close story1.txt?
ANS: (a) Line 6 (b) Line 4 (c) Line 10 (d) Line 7 (e) Line 2 (f) Line 13 (g) Line 6, Line 8 and Line 11 (h) Line 15
145 Aarti is new in python data-handling. Please help her to count the number of lines which begins with ‘W’ or ‘w’ in 4
poem.txt. (a) # Line 1 : To open file POEM.txt in read mode (b) # Line
2 : To check first character of every line is ‘W’ or ‘w’. (c) # Line 3 : To increase the value of count by 1. (d) # Line 4 :
To call the function count_poem.
146
4
Mohan has written the following code: (a) What will be the output? (i) It will
display first 20 characters. (ii) It will display 20th character and onwards. (iii) It will display first 20 bytes.
(iv) It will display content at 20th byte. (b) Meenu, Mohan's friend has written the following Python
code. What will be the output? (i) True (ii) False (iii)
None (iv) Error (c) The read() method return ____________. (i) str (ii) list (iii)
tuple (iv) None of these (d) The readline() method returns ______________. (e) The
readlines() method returns _______________.
ANS: (a) (iii) It will display first 20 bytes. (b) (i) True
147 Mr. Iqbal is a programmer, who has recently joined the company and given a task to write a python code to perform
the following binary file operations with the help of two user defined functions/modules:
(a) Add() to create a binary file called school.dat containing student information – roll number, name,age and
marks of English, Maths and Science(out of 100) of each student. 5
(b) Display() to display the information of all students. He has succeeded in writing partial code and has missed
out certain statements, so he has left certain queries in comment lines. You as an expert of Python have to provide
the missing statements.
(a) Write the
statement to import module. (b) Write the statement to append the record in binary. (c) Write the statement to
specify object. (d) Write the statement to store data in st. (e) Statement to close the five object.
ANS: (a) import (b) ab (c) file (d) load (e) close()
148 Mr. Mathew is a programmer, who has recently joined the company and given a task to write a python code to
perform update operation in binary file (stud.dat) which contain admno (Admission No) and fee(Fee paid by
student). 5
He has succeeded in writing partial code and has missed out certain statements, You as an expert of Python have
to provide the missing statements.
(a) # statement 1 to import
module/package (b) # statement 2 to open binary file. (c) # statement 3 to store
file pointer position (d) # statement 4 to place the file pointer at specified
position (e) # statement 5 to store the data. (f) Which of the following commands is used to
write the list LST into the binary file (FILE is file object)? (i) pickle.write(LST,FILE) (ii)
pickle.write(FILE, LST) (iii) pickle.dump(LST,FILE) (iv) FILE=pickle.dump(LIST)
(c) file.tell()
(d) file.seek(pos) (e) pickle.dump(st,file) (f) (iii) pickle.dump(LST,FILE)
149 You are a student in CBSE School. Teacher has given a task to write a python code to perform the following binary
file operations with the help of two user defined functions/modules:
(a) addrecord() to create a binary file called school.dat containing student information (in list data-type) – student 5
number, name marks (out of 100) of each student.
(b) search() to display name and marks of the student by asking the student number form the user. You need to
write the code in the missing statement (Fill in the blank)
Write the statements for the following: (a) #
statement 1 to declare the data-type (b) # statement 2 to store all records in stu (c)
ANS: (a) Statement 1 : student = [] (b) Statement 2 : stu = pickle.load(f)
(c) Statement 3 : student.append(s)
150 Dhruv Kumar of class 12 is writing a program to create a CSV file “Teacher.csv” which will store the teacher
information entered by user. He has written the following code. As a programmer, help him to successfully execute
the given task. Write appropriate statement / function/ code against fill ups.
152 Krishna is confused in Python code given below. Please help him to answer the following questions.
(e) Line 2
153 Aarti is new in python data-handling. Please help her to count the number of lines which begins with 'W' or 'w' in
poem.txt. POEM.txt This is my BOOk. Where is wish yours? My BOOK is
lying on the table. whom do we concern? def count_Poem(): file1 =
open("____________","_______") # Line 1 str1=file1.readlines() count=0 for r1
in str1: print(r1) if _________________ : # Line
5
2 _______________ # Line 3 print(count) file1.close()
_______________ # Line 4 (a) # Line 1 : To open
file POEM.txt in read mode (b) # Line 2 : To check first character of every line is 'W' or 'w'.
(c) # Line 3 : To increase the value of count by 1. (d) # Line 4 : To call the
function count_poem.
file1 = open("POEM.TXT","r")
ANS: (a) It will display first 20 bytes. (b) True (c) str
(d) str
(e) list
155 You are a student in CBSE School. Teacher has given a task to write a python code to perform the following binary
file operations with the help of two user defined functions/modules:
(a) addrecord() to create a binary file called school.dat containing student information (in list
data-type) – student number, name marks (out of 100) of each student.
(b) search() to display name and marks of the student by asking the student number form 5
the user. You need to write the code in the missing statement (Fill in the blank) import pickle
def addrecord(): student=___________ f=open("student.dat","rb+")
stu=pickle._________(f) for i in stu: student.append(i)
f.close() ch='y' while ch=='y': stno=int(input("Enter
student number")) sname=input("Enter Name")
marks=int(input("Enter Marks")) s=[stno,sname,marks]
student.____________(s) ch=input("Want to add more(y/n) ?")
f=open("student.dat","________") pickle.dump(student,f) f.close()
def search(): f=open("student","rb") stu=pickle.load(f)
found=0 r=int(input("Enter the student number to be search")) for i in stu:
if i[0]==________: print("Record Found")
print(i[1],":",i[2]) found=1 ___________ if found==0: print("No record
found") f.close() Write the statements for the following: (a) # statement 1 to declare
the data-type (b) # statement 2 to store all records in stu (c) # Statement 3 function to insert
data at end. (d) # statement 4 to save the data (e) # Statement 5 to search for student
number.
156 Dhruv Kumar of class 12 is writing a program to create a CSV file "Teacher.csv" which will store the teacher
information entered by user. He has written the following code. As a programmer, help him to successfully execute
the given task. Write appropriate statement / function/ code against fill ups.
import ___________
157 Krishna of class 12 is writing a program to read the details of Sports performance and store in the csv file
"games.csv" delimited with a tab character. As a programmer, help him to achieve the task.