0% found this document useful (0 votes)
14 views

Unit Test - 1 Feb 26

Uploaded by

raghavkrishna.b
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)
14 views

Unit Test - 1 Feb 26

Uploaded by

raghavkrishna.b
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/ 5

SECTION – A 1 X 18 = 18

1. State True/False.
An absolute path always begins with a root folder.
2. Which of the following method is used to serialize the
Python objects?
a) load b) dump c) write d) writerow
3. Read the code given below and find which of the following
is the content of the file, if the file contains ‘GOOD’
before execution?
With open(‘main.txt’,’w’) as File:
File.write(‘BYE’)

a) GOODBYE b) BYE c) GOOD d) Error


BYE
4. Which of the following python statement moves the file
pointer 15 bytes ahead from the current position of file
pointer in the file referred by the file object File.
a) File.seek(-15,2)
b) File.seek(15)
c) File.seek(0,15)
d) File.seek(15,1)
5. Which of the following method forces Python to write the
contents of file buffer on to storage file?
a) close b) flush c) break d) closed
6. The file access mode used for exclusive creation of a
file is _______.
a) w b) a c) x d) w+
7. To open a file c:\scores.txt for reading, we use
_____________
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)
8. What will be the output of the following Python code?
f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break
print(f.closed)
a) True b) False c) None d) d) Error
9. What happens when the following code is executed and
file1.txt is not present in the current directory?
File_obj = open(‘file1.txt’, ‘r’)
a) Python creates a new file b)Raises a FileNotFoundError
c) Raises an IOError d) Does nothing
10. Which of the following is the default mode to move
the file pointer to a specified position?
a) 0 b) 1 c) 2 d) None of these
11. Find the statement which causes error in the
following Python code.
import pickle #line 1
FOUT=open(‘Deposit.dat’,’rb’) #line 2
D=pickle.dump(FOUT) #line 3
FOUT.close() #line 4
12. Read the code below and find the output if the file
‘Quotes.txt’ contains the below content.
If you always try your best,
Then you’ll never have to wonder

With open(‘Quotes.txt’,’r’) as Myfile:


Myfile.readline()
Print(Myfile.tell())
a) 28 b) 29 c) 30 d) 27
13. What would be the data type of the variable data in
the following statements?
a) data = f.read(10)
b) data = f.readlines()
14. In order to work with standard I/O stream, we need to
import _____ module.
15. _____ function returns the current working directory.
16. State True/False.
Specific programs are not required to interpret and
Access the content of a binary file.

Assertion and Reasoning based questions:


(i) Assertion (A) and Reasoning (R) are True and R is
the correct explanation of A.
(ii) Assertion (A) and Reasoning (R) are True and R is
not the correct explanation of A.
(iii) A is True but R is False
(iv) A is False but R is True
17. Assertion (A): The file modes ‘r’,’w’,‘a’ also reveal
the type of file these are being used with.
Reasoning (R): The binary file modes have ‘b’ suffix with
regular file access modes.
18. Assertion (A): A binary file in python is used to
store collection objects like lists and dictionaries that
can be later retrieved in their original form using
pickle module.
Reasoning (R): Binary Files are just like normal text
files and can be read using a text editor like notepad.
SECTION – B 2 X 7 = 14
19. Write any 2 significant differences between text
file and binary file.
20. Write the use of ‘with’ statement’ in Python.
21. Consider the following code.

File=open(‘test.txt’,’w+’)
File.write(‘0123456789abcdef’)
File.seek(13) #statement 1
Print(File.read(3)) #statement 2
File.close()

Explain statement 1 and give the output of statement 2


22. State any 2 reasons for why it is advised to close a
file after we are done with the read and write operations
in a file?
23. Observe the following code and answer the questions
that follow.

File=open(‘Mydata’,’a’)
File.______________ #Blank 1
File.close()

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


(ii) Complete Blank 1 to write the list below into the
file ‘Mydata’.
[‘Credit’,’Debit’,’Insurance’]
24. How ‘r+’ mode differs from ‘w+’ mode?
(or)
Write the use of flush() method in Python.
25. Write a Python program to find and display the total
number of lines stored in a text file ‘Poem.txt’.
SECTION – C 3 X 5 = 15
26. Write a function rep_space() in Python to replace
all the blankspaces with ‘-‘ sign in the text file
‘merge.txt’.
27. Write a Python code to insert records in
“g_meet.dat” binary file until the user presses ‘n’. The
information is Google meeting id, Google meeting time and
class.
28. A) State any 2 differences between the text and
binary file.
B) Write the file access modes in which the existing data
will not be lost.
29. (i) State any 1 reason to think binary files are
better than Text files?
(iii) How are open() different from close() function
other than their functionality of opening and
closing the files?
30.Write a function filter(oldfile,newfile) to read the
file ‘source.txt’ and copy only those lines which do not
contains ‘@’ sign into another file named ‘target.txt’.

SECTION – D 4 X 2 = 8
31.Consider a binary file ‘vehicles.dat’ contains the
records in the following structure.
{‘Vcode’:value,’Vtype’:value, ‘perkm’:value}.
Complete the missing statement in the following python
code to read and copy only the ‘Deluxe’ type of vehicles
into the file ‘Deluxe,dat’.

import _______________ # line 1


def copyfile():
fin=_________________________ # line 2
fout= open(‘Deluxe.dat’,’wb’)
c=0
R=_______________ # line 3
for x in R:
if x[‘Vtype’] = ‘Deluxe’:
pickle._____________ # line 4
c+=1
if(c==0):
print("Record not found")
else:
print(c, records copies successfully")
fin.close()
fout.close()
copyfile()
(i) Complete line 1 to import the necessary module.
(ii) Complete line 2 to open the file ‘Vehicles.dat’.
(iii) Complete line 3 to read data from the file
‘Vehicles.dat’
(iv) Complete line 4 to write the record into the file
‘Deluxe.dat’

32.(i) Write a function in Python to count and display


the number of lines which starts with the alphabet
‘M’.(Ignoring case) (3+1)
(ii) Write a Python statement to open the file
‘Records.txt’ in read and append mode.

SECTION – E 3 X 5 = 15
33.A binary file ‘Books.dat’ has the structure
[BookNo, BookName, Author, Price]
Write a user defined function Create_File() to insert a
record and add to “Books.dat”. Also write a function
CountRec(Author) in Python which accepts the author as
parameter and count and returns the number of books
written by the given Author. If no record is found,
display the appropriate message.
34.A) Write a function DISPLAYWORDS() in Python to read
from a text file ‘POEM.TXT’ and display those words
which are less than 4 characters.
B) Write the syntax to open a file using ‘with’ clause.
State its advantages.
35.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
for stock is an integer.
Write a Python program that defines and calls the
following user defined functions.
A) ADD_DATA() – To add the records into the file
‘PRODUCTS.DAT’ as long as the user wants.
B) UPDATEREC()- To update the file with a new value of
stock. The stock and the prod_code, whose stock is to
be updated, are to be inputted during the execution of
the program.

You might also like