0% found this document useful (0 votes)
5 views4 pages

File Data Test1

The document outlines a series of programming tasks and questions related to Python, including file handling, CSV operations, and data serialization using the pickle module. It contains exercises for writing code to manipulate records in CSV and binary files, as well as theoretical questions about file operations and Python syntax. The tasks are aimed at assessing the understanding of file I/O, functions, and error handling in Python.

Uploaded by

tutor nag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views4 pages

File Data Test1

The document outlines a series of programming tasks and questions related to Python, including file handling, CSV operations, and data serialization using the pickle module. It contains exercises for writing code to manipulate records in CSV and binary files, as well as theoretical questions about file operations and Python syntax. The tasks are aimed at assessing the understanding of file I/O, functions, and error handling in Python.

Uploaded by

tutor nag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Roll No : Name of School : Time - MM- 23

Date : Name of Assessment :


Subject :
Class :

1 Write a program to save the following records in laps1.csv. 3


ADMNO NAME AGE
1151, Ajay, 12
1152, Chandrika, 14
2 Define a function remove_newline() which will remove new line from sample.txt 3

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

(i) What type (Text/Binary) of file is Mydata?


(ii) Fill the Blank 1 with statement to write “ABC” in the file “Mydata”.
12 Which of the following is not a correct Python statement to open a text file 1
"Notes.text" to write content into it?[CBSE 2021]
(a) F=open('Notes.txt','w')
(b) F=open('Notes.txt','a')
(c) F=open('Notes.txt','A')
(d) F=open('Notes.txt','w+')
13 What is the significance of the seek() method? [CBSE 2021] 1
(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 offset within the file.
(d) It seeks the entire content of the file.
14 Suppose the content of a text file Notes.txt is: 1
"The.way to get started is to quit talking and begin doing"
What will be the output of the following Python code? [CBSE
2021]
F=open (Notes.txt")
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
15 Which mode is used to open a binary file for writing? 1
(a) 'r'
(b) 'rb'
(c) 'wb'
(d) 'w+'
16 Which of the following is used to import the pickle module? 1
(a) import binary
(b) import pickle
(c) import csv
(d) import file

17 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 'seek()' method is used to move the file pointer to a specified
position in the file.
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

You might also like