File Data Test1
File Data Test1
3 Write a function addrecord() a add new record to the binary file “employee” using list. 3
The list should consist of employee number, employee name and salary.
4 In the following questions, a statement of assertion (A) is followed by a statement of 1
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.
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
5Nisha, an intern in ABC Pvt. Ltd., is developing a project using the csv module in 1
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"
#csv File Content
ENO,NAME,DEPARTMENT
012
E1,ROSHAN SHARMA, ACCOUNTS
E2,AASMA KHALID, PRODUCTION
E3,AMRIK GILL, MARKETING
E4,SARAH WILLIAMS,HUMAN PERSOURCE
#incomplete Code with Errors
import CSV#Statement-1
with open (____, ____' newline='')as File:#Statement-2
ER = csv:____ #Statement-3
for.R in range(ER) : #Statement-4
if____=="Accounts" : #Statement-5
print(__, __) #Statement-6
(a) Nisha gets an Error for the module name used in Statement-1. What should
she write in place of CSV to import the correct module?
(b) Identify the missing code for blank spaces in the line marked as Statement-2
to open the mentioned file.
(c) Name the function name (with parameter) that should be used in the line
marked as Statement-3.
(d) Nisha gets an Error in Statement-4. What should she write to correct the
statement?
(e) Write the suitable code for blank space in Statement-5 to match every row's
3rd property with "ACCOUNTS".
6 Mr. Mathew is a programmer, who has recently joined the company and given a task 1
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.
# Update a record.
import ___________
st={}
flag=0
file=open("__________","rb+")
search=int(input("Enter Admission Number whose fee is to be updated : "))
amt=int(input("How much to be increased : "))
try:
while True:
Try:
pos=file.___________
st=pickle.load(file)
if st['admno'] == search:
st['fee']+=amt
file._______(pos)
pickle._______(st,file)
print("Record Updated")
flag=1
except EOFError:
if flag==0:
print("Record Not Found")
file.close()
(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)
7 Evaluate the following expressions: 1
(i) (5 + 8) * 3 - 6 / 2
(ii) (2 + 3) * 5/4+(4 + 6)//2
(iii) 12 + (3 * 4 – 6) / 3
(iv) 12 + (3 **4 - 6)// 2
(v) 12 * 3 % 5 + 2 ** 6//4
(vi) 12 % 5 **3 +(2*6)//4
8 What is the syntax of remove() a file in python? 1
(a) remove(file_name)
(b) remove(new_file_name, current_file_name,)
(c) remove(() , file_name))
(d) none of these
9 What is the pickling? 1
(a) It is used for object serialization
(b) It is used for object deserialization
(c) None of the mentioned
(d) All of the mentioned
10 Which of the following statement is not true regarding the opening modes of a file? 1
(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.
(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.
11 Observe the following code and answer the questions that follow: 1