Question Bank of Multiple-Choice Questions 2021-22 Class Xii Subject Computer Science - Chief Patron Patron
Question Bank of Multiple-Choice Questions 2021-22 Class Xii Subject Computer Science - Chief Patron Patron
CHIEF PATRON
SRI K. SASEENDRAN, DEPUTY COMMISSIONER
PATRON
DR (SMT) V. GOWRI, ASSISTANT COMMISSIONER
CO ORDINATOR
3
Which of the following operator can be used with string data type?
1. ** 2. % 3. + 4. /
Answer is:3
4 Consider a tuple T = (10, 15, 25, and 30). Identify the statement that will result in an error.
1. print(T[2]) 2. T[3] = 19 3. print(min(T)) 4. print(len(T))
Answer is: 2
5
Which of the following symbol is used in Python for Multiline comments line comment?
1. /*** 2. /* 3. „‟‟ 4. #
Answer is:
6
Identify the output of the following Python statements.
3
y = x[1][2] print(y)
Answer is: 3
7
Identify the output of the following Python statements.
L.insert( 3, 4)
L.insert( 2, 3)
print (lst1[-5])
1. 2 2. 3 3. 4 4. 20
Answer is:2
8
Which of the following properly expresses the precedence of operators (using parentheses) in
the following expression: 5*3 > 10 and 4+6==11
Answer is : 1
9
What will be the output of the following Python code?
i=1
while True:
if i%0O7 == 0:
break
print(i)
4
i += 1
1. 1 2 3 4 5 6
2. 1 2 3 4 5 6 7
3. error
Answer is: 1
10
values = [1, 2, 3, 4]
numbers = set(values)
def checknums(num):
if num in numbers:
return True
else:
return False
print i
1. 1 2 3 4 2. 1 2 3. 3 4 5. 2 3 4
Answer is:1
import ___________________ #1
AR=[20,30,40,50,60,70]
FROM=random.randint(1,3)
5
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”#“)
Answer is: ii
2
What will Be the maximum value of the variables FROM and TO?
Answer is : I
3
What will Be the minimum value of the variables FROM and TO?
Answer is: ii
4
What possible outputs(s) are expected to be displayed on screen at the time of execution
of the program?
Answer is: ii
5
What will be the output of random.random ( )
Answer is : ii
6 Ramu write a list program as shown below .based on the code answer the questions
print(m) #2
v = m[0][0]
print(v) #3
for row in m:
for element in row:
if v < element:
v = element
return v
print(fun(data[0])) #4
7
What will be printed after execution of the line #2 ?
(i) [1,2] (ii) [[1, 2], [3, 4]] (iii) [[5, 6], [7, 8]] (iv) [5,6]
Answer is:ii
8
What will be printed after execution of the line #3 ?
Answer is: iv
9
What will be last line of the output after execution of the line #4 ?
Answer is: i
10
What will be the last line of the output of the above code, if line #4 is replaced with print
(fun (data (1)) ?
Answer is: i
7
3 Natasha is working in Python program which is function oriented. She is using the
functions already available in python. These functions are called:
(a) User defined functions
(b) In-built functions
(c) module functions
(d) reusable functions
Ans: (b)
5 Pick one the following statements to correctly complete the function body in the given
code snippet.
def f(number):
# Missing function body
print(f(5))
(c) print("number")
(d) return number
Ans (d)
6 Consider the following program. What is the correct flow of execution of statements:
import random
x = random(2,6)
y = random(1,2)
z = x + y
print(z)
8 Which is NOT the possible output of following program from given options:
import random
periph = ['Mouse', 'Keyboard', 'Printer','Monitor']
for i in range(random.randint(0,2)):
print(periph[i],'*',end=" ")
Lalit is a game programmer and he is designing a game where he has to use different
python functions as much as possible. Apart from other things, following functionalities are
to be implemented in the game.
(1) The players have to input their names and Lalit has to remove the unnecessary blank
spaces from the name.
(3) Since the program becomes too lengthy, Lalit wants a separate section where he can
store all the functions used in the game program.
(4) He wants to implement usage of less memory so he doesn‟t want to include all the
functions stored in separate sections.
(5) In the game, one source object generates and throws balls and the player has to catch
the balls. Here the distance and time is to be calculated so that the program can check
whether the ball was caught or missed by the player.
Lalit is feeling difficulty in implementing the above functionalities. Help him by giving
answers following questions:
11 Q.1: In functionality (1), which python module and function should be used:
(A) remove() function of string module (B) split function()
(C) trim() function of string module (D) strip() function of string module
One student who is learning Python, is making a function-based program to find the roots
of a quadratic equation. He wrote the program but he is getting some error. Help him to
complete the task successfully:
1 from ……… import sqrt
2 Def quad(b,c,a=1):
3 x = b*b-4*a*c
4 if x<0:
5 return "Sorry, complex root(s)"
6 d = sqrt(x)
7 r1 = (-b + d)/(2*a)
8 r2 = (-b - d)/(2*a)
9 return r1,r2
10 print(quad(1,1,2))
11 root = quad(3)
12 rt = quad(2,1)
20 Q.5: If all the errors are removed from the program then what will be the value of variable
rt after execution of line 12
(A) (-1.0, -1.0) (B) [-1.0, -1.0]
(C) "Sorry, complex root(s)" (D) -1.0, -1.0
11
1 def s_interest(prnc,time=3.rate=0.12):
return (prnc*time*rate)
s_interest(5000)
a. 800
b. 1700
c. 1800
d. 450
Ans : c. 1800
2 def func(*args):
for i in args:
print(i)
func(1, 2, 3)
a. 1
2
3
b. 1 2 3
c. Error
d. None
Ans a. 1
2
3
3 def greet(x):
if x < 0:
return "Welcome!"
else:
return "Namaste"
print(greet(1))
12
print(greet(-1))
a. Namaste
Welcome!
b. Welcome!
Namaste
c. 1!
-1
d. Error
Ans : a. Namaste
Welcome!
Ans C. None
func1(max, [7,8,9])
func1(min, [8,7,9])
a. 9,7
b. 7,9
c. error
d. None of the mentioned
Ans: a) 9,7
1 Shivam wants to know the correct name resolution rule in Python.sPlease help him.
2 Which type of parameter Mahesh has to use in function definition, so that any number of
a. Keyword
b. Variable length.
14
c. Positional
d. Default.
Ans : b. Variable Length
3 Where does the execution of the program start?
a. user defined function
b. __main__
c. void function
d. __name__
Ans : b. __main__
4 1.def cat_dog_count(arg):
2. arg=arg.lower()
3. l=arg.split()
4. c1=l.count("cat")
5. c2=l.count("dog")
6. if c1==c2:
7. return "True"
8. else:
9. return "False"
10.b=cat_dog_count("ca dog cat do d Trip flip cat")
11.print(b)
a. 1 2 3 4 5 6 7 8 9 10 11
b. 1 10 1 2 3 4 5 10 11
c. 1 10 1 2 3 4 5 6 7 10 11
d. 1 10 1 2 3 4 5 6 8 9 10 11
Ans d. 1 10 1 2 3 4 5 6 8 9 10 11
5 Which of the following function call can be used to invoke the below function definition?
def calc(p,q,r,s)
I. calc(3,4,5,6)
II. calc (p=1,2,3,5)
III. calc(3, 4, r=3, s=5)
IV. calc(q=4, p=3,s=5,r=7)
8 Rohit wants to access the global variable inside the function having the local variable same
name as Global variable. Suggest him which keyword he has to use:
a. import
b. assert
c. lambda
d. global
Ans: d. global
9 def power2(n=3):
def f(x):
return n**x
return f
print(power2()(4))
a. Error
b. 3
c. 4
d. 81
Ans d.81
10 def double(x):
return 2*x
def apply(f,data):
return f(data)
print(apply(double,[1,2,3]))
a. [1, 2, 3, 1, 2, 3]
b. [2,4,6]
c. 2*[1,2,3]
d. Error
Ans a.[1, 2, 3, 1, 2, 3]
16
4 Mr SHASHIDHAR BOLARUM
Introduction to files, types of files (Text file, Binary file, CSV file
c) End of line
8
A file maintains a __________ which tells the current position in the file where
writing or reading will take place.
17
9
Which of the following file modes opens a file for appending and reading in a
binary file and moves the files pointer at the end of the file if the file already
exists or creates a new file?
10
Which method of pickle module is used to write onto a binary file?
a. dump()
b. load()
c. All of the above
d. None of the above
Answer : a) dump()
1 Kum. Aishwarya is running her own boutique business. She wants to store data of all
orders permanently and fast processing of data through her boutique software. Suggest
her to choose the appropriate technique among the following.
a) She can use Python Dictionaries with Text files.
b) She can use Python Dictionaries with Binary file concept.
c) She can use Python Lists without the Binary files concept.
d) She can use Python Dictionaries without the Binary file concept.
Answer : b) She can use Python Dictionaries with Binary file concept.
3
Master Adithya is a class 12 student like you. He is practicing text file
programming. He has a text file named names.txt. He wants to display all
18
the names contained in the text file. He has written one of the following
codes and succeeded in getting the output. Guess which among the
following option might have given the correct output.
print(names) print("line")
print(line) print(line)
Answer : b)
4
A Student's windows O/S got corrupted. He is trying to access his files through
Command Prompt, but unable to find out all his Binary data files pertaining to his
project. Help him to find out all his binary data files by suggesting the suitable
extension name among the following.
Anser c) .dat
5
Given the following directory structure. Assume that the CWD is in the root folder
where animals directory resides. What is the relative path to the feline folder?
19
Answer : b)
6
Given the following directory structure. Assume that the CWD is in the feline folder.
What is the relative path to the file bears.gif?
Answer : c)
7
Given the following directory structure. Assume that the CWD is in the root folder
where animals directory resides. What is the absolute path to bears.gif?
a) C:/animals/ursine b) ../animals/feline c)
20
Answer : c)
8
Given the following directory structure. Assume that the CWD is in the feline folder
what is the relative path to lions.gif?
Answer : b)
9
Master Adithya could able to read contents of a text file part by part. Out of the
following which option he would have been used?
Answer c)
10
In a program, there is a need of reading whole content of a textfile in a single shot
and store it in the form of list. Suggest the best option among the following
Answer c)
21
1
What is the default mode when the file is opened using the open() method ?
a. Write
b. Read
c. Write and read
d. Read and write
2
The function read() is used to
3
Which of the following statements is true with respect to closing a file
Correct Answer : c) Python automatically closes a file if the reference object of the file is
allocated to another
4
Which of the following statements is true?
a. Python doesn't write data to the file until you close the file using the close() method
b. If the file is opened for writing and is not closed, data is not written into the target
file.
c. Python can write data to the file even if you do not close the file
22
Correct Answer : b) If the file is opened for writing and is not closed, data is not written into
the target file
5
The mode “w+” is used to open the file for
6
:When a file is opened in “w” mode, the file pointer is placed at
7
:When a file is opened in “a” mode, the file pointer is placed at
8
(A) : If a file is opened using the “with” statement, you get better syntax and exceptions
handling.
(B): When a file is opened using the “with” statement, it need not be closed using the
close() function.
9
In r+ mode, if we write the file directly, it will
10
When the file content has to be modified, we can use the ____________ mode
a. r
b. w
c. a
d. r+
1
Rajitha, during Practical Examination of Computer Science, has been assigned an
incomplete search() function to search in a text file “CAMP.txt”. The file “CAMP.txt” is
created by his Teacher and the following information is known about the file
• File contains details of adventure activities like caving, trekking, paragliding, rafting
and rock climbing Rajitha has been assigned the task to complete the code and print
the number of the word trekking
def search():
f = open("CAMP.txt",____) #Statement-1
A=____________________ #Statement-2
ct=0
for x in A:
24
p=x.split()
if p==”trekking”:
ct+=1
a) r b) r+ c) rb d) wb
Correct Answer: a) r
2 Name the function that can be used by Rajitha to read the content of the file in statement
-2.
2 Rahul is assigned the task of storing the information of his class mates as student records
in a text file “Stu2021.txt”.He wants to create a file to write the records.
Which of the following is not a valid mode to open a file for writing the data?
(a) w (b) r+ (c) r (d) w+
Correct Answer: (c) r
3 Rohith of class 12 is asked by his Computer Science teacher to count the number of “the”
and “and” in a text file “Book.txt”. He was able to do the task to some extent, but was
confused in some areas. Help him out to complete the task
def displaytheand():
num=0 _________ #Statement 1
N=f.read()
M=N.split()
for x in M:
if x=="the" or x== "and":
print(x) num=num+1 __________ #Statement 2
print("Count of the/My in and:",num)
1. Which of the following is the correct one to be used in the place of statement 1
a) f=open("Book.txt","w")
b) f=open("Book.txt","r")
c) f=open("Book.txt","a")
d) F=open("Book.txt","r")
Correct answer : b
2. Identify the correct statement that may be used in the place of statement 2
a) f.close()
25
b) f.close(“book.txt”)
c) close()
d) None of the above
Correct answer : a
4 Ajay is studying in an Engineering College in CSE branch. His sister, a class 12 student of
Computer Science comes to him one day asking him the difference between r+ and w+
modes of a file. What is the correct answer Ajay would give to his sister?
b) f.close()
c) F.close(“passion.txt”)
d) No statement is required
Correct answer : b
7
Which of the following statements is not True
b) Python automatically closes a file when the reference object of a file is reassigned to
another file.
c) close() method returns a value which ensures the termination of the file stream
Correct answer : c
8
What is the expected output of the given code?
f = None
if i > 2:
break
print(f.closed)
Correct Answer: a
9
Which of the following statements is true with respect to the files in Python?
a) File can be opened with a file handle using open method without any arguments
b) File can be opened with a file handle using open method with one argument to read the
file
27
c) File can be opened with a file handle using open method with one argument to write the
file
d) File can be opened with a file handle using open method with one argument to append
the file
Correct answer : b
10 To open a file only for reading which of the following statement cannot be used :
a) f = open("PYTHON.txt")
b) f = open("PYTHON.txt”, "rt")
c) f = open("PYTHON.txt", "r")
d) f = open("PYTHON.txt",”r+”)
Correct answer : d
28
1
The ____________ method returns a list containing each line of the file as a list item .
2
If we want to write a sequence of strings,list, tuple into the file then we use
_________ function.
3
read() and readline() functions can be used for reading no of characters from file if
size is mentioned.
4
We can use readline( ) function which can read one line at a time from the file.
5
To read twelve characters from a file object fin we use ________
6
Python automatically flushes the file buffers before closing a file with close()
function.
a) True b) False
29
Answer : a) True
7
The ________ method in Python file handling clears the internal buffer of the file. .
8
Suppose content of 'Myfile.txt' is
myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close()
a) 24 b) 25 c) 26 d) 27
Answer : c) 26
9
To create a new file or to write on an existing file after truncating / overwriting its old
content , file has to be opened in _______ access mode
Answer : a) “w+”
10
Which of the following options can be used to read the first line of a text file Myfile.txt?
11
Which of the following options can be used to read the whole content of a text file
Myfile.txt?
12
To open a file for reading any of the following statement can be used :
a) f = open("demofile.txt")
b) f = open("demofile.txt", "rt")
c) f = open("demofile.txt", "r")
d) All of the above
Answer : d) All of the above
13
Suppose content of 'Myfile.txt' is:
myfile = open("Myfile.txt")
data = myfile.readlines()
print(len(data))
myfile.close()
a) 3 b) 4 c) 5 d) 6
31
Answer : b) 4
14
Suppose content of 'Myfile.txt' is
myfile = open("Myfile.txt")
x = myfile.read()
y = x.count('the')
print(y)
myfile.close()
a) 2 b) 3 c) 4 d) 5
Answer : b) 3
1
Atif has been asked by his senior to complete the following code. The code uses a
text file namely message.txt.
def count_words(filename):
Bigwords=0
F=open(filename,‟r‟)
Data=F.___________ # line 1
For w in words :
__________ # line 3
___________# line 4
_______ # line 5
32
i. Which function is used to read all the content of the file for line1 and store in
Data variable in string format.
iii. Which option is correct for completing line 3 and line 4 so that the count of
words having length more than 8 characters.
Correct Answer :
b) if len(w) >8:
bigwords+=1
iv. Which option is correct for completing line 5 so that function count_words() is
called.
c) big _count=count_words(“message.txt”)
d) big=count_words(“message.txt”)
count=count_words(“message.txt”)
2
Teacher has developed a program to count how many lines start with “T” in a text
33
def countlines():
__________# line 3
for I in data :
if _______ : # line4
c+=1
ii. Which function is used to read the content from the file
a) c b) count c) I d) data
Correct Answer :a) c
3
Kum.Anisha wants to develop a program to count the number of “Me” or “My”
words present in a text file “STORY.TXT” . But she is not sure about the code .
Help her to complete it.
34
def displayMeMY():
n=0
N = __read() #line2
M = N.________ #line 3
for x in M :
if ____________________: #line 4
n=n+1
a) w b) r c) rb d) a
Correct Answer : b) r
a) F. b) f. c) n. d) N.
Correct Answer : f.
4
Rahul is trying to perform write the data to a text file. He is not sure of the
commands to be used. Hence partially he could write the program . Help him to
complete the program.
file1._________(L) #line2
file1.close()
file1 = open("myfile.txt", _____)#line3 opening file to add new data to existing file
.
file1.write("\n")
file1._________("Today")#line4
i. Which access mode to be used in line 1 to open the file for writing on to
file.
a) w b) w+ c) wr+ d) a
iii. Which access mode to be used in line3 to open the file for writing new
content on existing file without any data loss.
a) w b) w+ c) wr+ d) a
Correct Answer : d) a
5
Smitha wants to copy text file “Source.txt” onto “target.txt” barring the lines starting
with a “@” sign. Help her in completing the code. def filter(oldfile, newfile):
fin=open(oldfile,___)#line1
fout=open(newfile,_____)#line2
while True :
text=__________#line3
if len(text)==0:
break
if text[0]==”@” :
continue
_____.write(text)#line4
fin._____ #line5
fout._____#line6
filter(“source.txt”,”target.txt”)
a) w b) r c) rb d) a
Correct Answer : b) r
a) w b) r c) rb d) a
Correct Answer : a) w
iii. Which of the following function can be used to complete line3 for
reading line by line.
a)fin.readline() b) fin.readlines()
c)fin.read() d) fout.readline()
37
v. Identify the function that can be used in line5 and 6 to close the files
6
Write the output of the following code
f = open("data.txt", "r")
d =read()
d1 = read(5)
print(d)
print(d1)
7
Your teacher has given you a method/function FilterWords() in python which reads
data from a text file NewsLetter.TXT. Your teachers intentionally kept few blanks
in between the code and asked you to fill the blanks so that the code will run to find
desired result. Do the needful with the following python code.
def FilterWords():
c=0
print(“No of lines”,linecount)
_________ #Statement-5
FilterWords()
a) rb b) r c) w d)a
Correct Answer : b) r
iii. Fill the statement 3 to read the remaining content of the file in list form.
iv. Fill the statement 4 to count the no. of lines in the file.
8
Renu wants to write multiple lines i.e 5 lines of text content into a text file
mylife.txt.But she is not able to do it. Help her to complete the program.
________________ #line 2
39
Line = ____________________#line 3
_____________ #line 4
_____________ #line 5
f.close()
iii. Fill line 3 with correct option to take the input from console IO.
iv. Fill line 4 with correct option to copy the line to file.
v. Fill line 5 with correct option to add new line character after every line.
9
Sonu wants to create one single program to read and write data using a single file
object.But she got confused and could not complete the program. Help her to
complete it . fileobject=open("report.txt", “____”) # line 1
40
while True:
fileobject.___________ #line2
fileobject.write('\n')
if choice in ('n','N'):
break
fileobject._________ # line3
print()
str=_______________ #line4
print(str)
___________ #line 5
i. Fill the line1 with appropriate access mode to perform both write and read
operations.
a) r+ b) w c) w+ d) a
Correct Answer : c) w+
iii. Fill line 3 with correct function that places file object at beginning of file
10
Mr.Ravi, has written code which reads each character of a text file story.txt,and
also counts and displays the occurance of alphabets of A (include small cases „a‟
too).But while doing so ,he is unable to complete the code ,help him to complete
the code.
Updated information
A or a :4
M or m : 2
Code :
def EUcount():
A=0
for x in r:
_________________# 3 statement
A=A+1
42
f.close()
print(“A or a:”,A)
1.Which of the following commands is used to open the file “Story.txt”? (marked as
#1 in the Python code)
a. f= open("story.txt",'w')
2.Which of the following commands is used to read the data from the file,
Story.txt? (marked as #2 in the Python code)
a. f.read( 20)
b. f.read()
c. f.write(L,F)
d. f=pickle.dump(L)
1 Which function is used to change the position of the File Handle to a given specific
position.
a. .seek()
b. read()
c. tail()
d. write()
Ans . A
2 Seek() function with negative offset only works when file is opened in -------- mode.
a. read mode
b. write mode
c. binary mode
d. All of these
Ans. C
Ans. A
Ans. B
5 How do you change the file position to an offset value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
Ans. A
44
Ans. D
7 If we open the file in read mode then the initial value of tell() is
a. -1
b. 0
c. 1
d. End of the file
Ans. A
Ans. A
Ans. D
Ans. A
45
1 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
Answer. A and B
Answer. B
5 To read the next line of the file from a file object fobj, we use:
(a) fobj.read(2)
(b) fobj.read()
(c) fobj.readline()
(d) fobj.readlines()
Answer. C
6 What happens if no arguments are passed to the seek function?
a) file position is set to the start of file
b) file position is set to the end of file
46
Answer: d
1 If a file is opened for reading, which of the following statements is not true?
a. The file must exist on the disk on the specified path
b. If the file exists at the specified path, the file is successfully opened.
c. The file even if at a different location on disk other than the specified path, will get
opened
d. Python gives error if the file does not exist at the specified path
Answer : c) The file even if at a different location on disk other than the specified
path, will get opened.
2
To read 24 characters from a file object infi, we use
a. Infi.read() b. infi.read(24)
c. Infi.readline() d. infi.readlines
Answer: b) infi.read(24)
3
The readlines() method returns__________
Answer : b) rw
5
Which of the following functions do you use to write data in the binary format?
a. Write() b. output()
c. dump() d. send()
Answer : c) dump().
6
Which of the following command is used to open a file “c:\path.txt” in read mode
only?
a. Fin=open(“c\path.txt”,”r”) b. fin=open(“c\\path.txt”,”r”)
48
c. Fin=open(file=”c:\path.txt”,”r+”) d. fin=open(file=”c\\path.txt”,”r+”)
Answer : fin=open(“c\\path.txt”,”r”)
7
Which of the following is not a correct statement for binary files?
a) Easy for carrying data into buffer
b) Much faster than other file systems
c) Characters translation is not required
d) Every line ends with new line character „\n‟
Answer : d) Every line ends with new line character „\n‟
8
Which of the following commands can be used to read the entire contents of a file
as a string using the file object <tmpfile>?
a. tmpfile.read(n)
b. tmpfile.read()
c. tmpfile.readline()
d. tmpfile.readlines()
Answer : b. tmpfile.read()
9
Which of the following command is used to open a file “c:\temp.txt” for writing in
binary format only?
a. outfile = open(“c:\temp.txt”, “w”)
b. outfile = open(“c:\\temp.txt”, “wb”)
c. outfile = open(“c:\temp.txt”, “w+”)
d. outfile = open(“c:\\temp.txt”, “wb+”)
10
Trying to open a binary file using a text editor will show:
a. Garbage values
b. ASCII values
c. Binary character
d. Unicodes
1 Ms. Suman is working on a binary file and wants to write data from a list to a binary
file. Consider list object as l1, binary file suman_list.dat, and file object as f.
i ) Which of the following can be the correct statement for her?
a) f = open(„suman_list‟,‟wb‟); pickle.dump(l1,f)
b) f = open(„suman_list‟,‟rb‟); l1=pickle.dump(f)
c) f = open(„suman_list‟,‟wb‟); pickle.load(l1,f)
d) f = open(„suman_list‟,‟rb‟); l1=pickle.load(f)
Correct Answer : a) f = open(„suman_list‟,‟wb‟); pickle.dump(l1,f)
ii ) Which option will be correct for reading file for suman?
a )f = open(„suman_list‟,‟rb‟)
b )f = open(„suman_list‟,‟r‟)
c )f = open(„suman_list‟,‟r+‟)
d )f = open(„suman_list‟,‟ab‟)
Correct Answer : a) f = open(„suman_list‟,‟rb‟)
iii ) In which of the file mode existing data will be intact in binary file?
a) a
b) ab
c) w
d) wb
Correct Answer : b) ab
iv ) Which one of the following is correct statement?
a) import – pickle
b) pickle import
c) import pickle
d) All of the above
Correct Answer : c ) import pickle
v) What are the binary files used for?
a. It is used to store data in the form of bytes.
b. To store data
c. To look folder good
d. None of these
Correct Answer : a) It is used to store data in the form of bytes
2
Ms. Sejal is working on the sports.dat file but she is confused about how to complete the
code to read the data from the binary file. Suggest a suitable line for her to fulfil her.
____________ # Statement 1
def sports_read ():
f1 = _______________ # Statement 2
_________________ # Statement 3
print(data)
f1. close ()
sports.read()
i ) Identify the suitable code for blank space in line marked as Statement-1.
50
a. pickle import
b. import pickle
c. import.pickle
d. None of these
Correct Answer : b) import pickle
ii ) Identify the suitable code for blank space in line marked as Statement-2.
a. open("sports.dat","wb")
b. open("sports.dat","r")
c. open("sports.dat","rb")
d. None of these
Correct Answer : c) f1 = open("sports.dat","rb")
iii ) Identify the suitable code for blank space in line marked as Statement-3.
a. data = pickle.load(f1)
b. data = pickle.dump(f1)
c. data = pickle.load(f)
d. data = pickle.dump(f)
Correct Answer: a) data = pickle.load(f1)
iv ) What is the description of `r+b` in binary mode?
a. read and write
b. write and read
c. read only
d. none of these
Correct Answer : a) read and write
v )Which of the following file modes will not delete the existing data in binary file ?
a. wb
b. w
c. a
d. ab
Correct Answer : d) ab
3
Saritha is trying to add data onto a existing binary file and is facing difficulty in
completing the code.Help her to fill the gaps in the code.
Incomplete Code:
import pickle
_____________________________ # Statement 1
recno=1
print()
51
#taking data from user and dumping in the file as list object
while True:
ename=input_____________________ # Statement 2
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(_____________) # Statement 3
recno=recno+1
if ans.lower()=='n':
print()
bfile.tell())
______() # Statement 4
i ) To open the file for writing the data in line marked as Statement-1.
a. bfile=open("empfile.dat","ab”)
b. bfile=open("empfile.dat","a”)
c. bfile=open("empfile.dat","wb”)
d. bfile=open("empfile.dat","w”)
Correct Answer : a) bfile=open("empfile.dat","ab”)
ii ) To accept employee name from the user in line marked as Statement-2.
a. input("\tEmployee Name : ")
52
b. input(Employee Name :)
c. input("Employee Name )
d. None of these
Correct Answer : a) input("\tEmployee Name : ")
iii ) Identify the suitable code for blank space in line marked as Statement-3.
a. edata,bfile()
b. edata,bfile
c. data,bfile
d. edata,file
Correct Answer: b) edata,bfile
iv ) Identify the suitable code for blank space in line marked as Statement-4.
a. bfile.close()
b. bfile.close
c. file.close()
d. none of these
Correct Answer : a) bfile.close()
v ) Which of the following is the correct syntax to read from a file using load
function ?
A. pickle.load(<filehandle>)
B. <object> - load.pickle(<filehandle>)
C. <object> - pickle.load(<filehandle>)
4
A Binary file Stock.dat has a structure [pno,pname,qty,price].A user defined function
Createfile() to input data for 3 records and add to stock.dat .There are some blanks
help in filling the gaps in the code:
Incomplete Code :
def createfile():
File=open(“d:\\Stock.dat”,‟____‟) #Statement 2
record=[pno,pname,qty,price]
53
_______________ # Statement 3
Print(“Record inserted”)
File.close()
Createfile()
i ) Identify the suitable code for blank space in line marked as Statement-1.
a. csv
b. CSV
c. pickle
d. PICKLE
Correct Answer : c) pickle
ii ) Identify the suitable code for blank space in line marked as Statement-2.
a. wb
b. ab
c. w
d. a
Correct Answer : b) ab
iii ) select correct statement to write data into file for Statement-3.
a. pickle.dump(record,file)
b. pickle.dump(record)
c. pickle.dump(file,record)
d. pickle.load(record,file)
a. save
b. close
c. end
d. write
b. False
c. true
d. TRUE
6
Ms.Anitha is unable understand what can be the output of the following code.Help
her in getting the output.
Import pickle
L=[20,40,50]
55
f=open(“list.dat”,‟wb‟)
Pickle.dump(l,f)
f.close()
f=open(“list.dat”,‟rb‟)
data=pickle.load(f)
f.close()
print(data)
[20,40,50]
b. [20,30,50]
c. [20,30,50]
d. No output
[20,40,50]
b. fobj=open("salary.dat","r")
c. fobj=open("salary.dat","r+")
d. fobj=open("data.dat","rb")
Correct Answer : a) fobj=open("data.dat","rb")
ii ) The module used in line2
a. PICKLE
b. pickling
c. pickle
d. None of these
Correct Answer : c) pickle
iii ) Identify the salary to be checked in the code marked as line-3.
a. 50000
b. 20000
c. 24000
d. 10000
Correct Answer: b) 20000
iv ) Which of the following File Modes creates a new file, if the file does not exist?
(choose one/more)
a. „r‟
b. „bw‟
c. „w‟
d. „a‟
Correct Answer : c) w
v ) What is true about Binary files
a. They are not human readable
b. the file extension is .dat
c. the file stores same format as held in memory.
d. All of the above
Correct Answer : d) All of the above
8
Mr.Rohan wants to modify salary of employee having a structure[eid,ename
,salary],but unable to fill the gaps in the code. Help him to complete the code
Import pickle
f = open('d:/student.dat','rb')
reclst = []
while True:
try:
57
rec = pickle.load(f)
except EOFError:
break
f.close()
if reclst[i]['eid']==r:
reclst[i]['salary'] = m
for x in reclst:
pickle.dump(x,f)
f.close()
9
A binary file sports.dat contains information in the following structure:( Event,
Participant )
A code is shown below which is incomplete that would read contents from the
58
sports.dat and creates a file named Athletic.dat copying only those records from
sports.dat where the event name is “Athletics”.
import pickle
ath ( f1 , f2 ) :
l = pickle.load ( f1)
for t in l :
if ( t [ 0 ] == “________________” ) : #line 1
pickle.__________ ( t , f2 ) #line 2
f1 = open ( “ sports.dat “ , “ rb ” )
f2 = open ( “ athletics.dat “ , “ wb “ )
f.close()
f1.close()
10
A function searchprod( pc) in python is created to display the record of a particular
product from a file product.dat whose code is passed as an argument. Structure of
product contains the following elements [product code , product price].There is
some problem in completing the code,help to finish the code:
f = ________('d:/product.dat','rb') #line1
flag = False
while True:
try:
rec = pickle.load(f)
print('Product code:',rec['pcode'])
print('Price:',rec['price'])
flag = True
except EOFError:
break
if flag == False:
f.close()
1
_____________ is the process of converting Python object hierarchy into a byte
stream so that it can be written into a file.
a) Pickling
b) Unpickling
c) Dumping
d) Loading
Answer: a
2
_______ is the process of reading from a binary file
a) Pickling
b) Unpickling
c) Dumping
d) Loading
Answer: b
3
___________ of pickle module will unpickle the data coming from the binary file.
a) load()
b) dump()
c) writer()
d) insert()
Answer : a
61
4
___________ of pickle module will pickle the data in the binary file.
a) load()
b) dump()
c) writer()
d) insert()
Answer : b
5
_______________ will return the current position of file pointer in the file
a) seek()
b) search()
c) tell()
d) print()
Answer : c
6
________ places the file pointer at the specified position in the open file.
a) seek()
b) search()
c) tell()
d) print()
Answer : a
7
F.seek(20,0) will move the file pointer 20 bytes in forward direction from beginning of file.
State True or False
a) True
b) False
Answer : a
8
F1.seek(-5,1) will move the file pointer 5 bytes backwards from end of file.
a) True
b) False
Answer : b
9
Syntax of seek function in Python is myfile.seek(offset, reference_point) where myfile is
the file object. What is the default value of reference_point?
a) 0
b) 1
c) 2
d) 3
Answer : a
10
Which of the following statements is true?
Answer : b
1
Archit wants to create and display a Binary file named “Myfile.DAT”. Complete the
missing code to open, create and display the file.
double=[]
for i in range(1,11):
double.append(2*i)
fo=__________________ #Line2
pickle.________________ #Line 3
fo.close()
fin=___________________ #Line4
result=________________ #Line 5
fin.close()
a) csv
b) pickle
c) binary
d) bin
Answer : b
2. Fill in the blank in Line 2 to open the file for writing the contents of the file.
a) open("Myfile.dat","w")
b) open("Myfile.dat","r")
c) open("Myfile.dat","wb")
d) open("Myfile.dat","rb")
Answer : c
3. Fill in the blank in Line 3 with the function to write entire contents to file.
a) load(double,fo)
b) dump(double,fo)
c) writer(double)
d) insert(double,fo)
Answer : b
64
4. Fill in the blank in Line 4 to open the file for displaying contents of file.
a) open("Myfile.dat","w")
b) open("Myfile.dat","r")
c) open("Myfile.dat","wb")
d) open("Myfile.dat","rb")
Answer : d
a) pickle.read(fin)
b) pickle.readline(fin)
c) pickle.readlines(fin)
d) pickle.load(fin)
Answer: d
65
2
Rohith has been given the following incomplete code for entering his
details(Name,contact number and address) to a file “Personal.DAT” and display
the contents. Complete the missing code to open, create and display the file.
mydata=[]
name=input(“Enter Name:”)
address=input(“Enter address:”)
mydata=[name,contactno,address]
f1=__________________ #Line2
pickle.________________ #Line 3
f1.close()
f2=___________________ #Line4
result=________________ #Line 5
f2.close()
a) csv
b) pickle
c) binary
d) bin
Answer : b
2. Fill in the blank in Line 2 to open the file for writing the contents of the file.
a) open("Personal.dat","w")
b) open("Personal.dat","r")
c) open("Personal.dat","wb")
d) open("Personal.dat","rb")
Answer : c
3. Fill in the blank in Line 3 with the function to write entire contents to file.
66
a) load(mydata,f1)
b) dump(mydata,f1)
c) writer(mydata)
d) insert(mydata,f1)
Answer : b
4. Fill in the blank in Line 4 to open the file for displaying contents of file.
a) open("Personal.dat","w")
b) open("Personal.dat","r")
c) open("Personal.dat","wb")
d) open("Personal.dat","rb")
Answer : d
a) pickle.read(f2)
b) pickle.readline(f2)
c) pickle.readlines(f2)
d) pickle.load(f2)
Answer: d
67
3
You are provided with some incomplete code for entering student‟s details (Rollno,
Name and marks) to a file “Student.DAT” and display the contents. Complete the
missing code to open, create and display the file.
data=[]
name=input(“Enter Name:”)
marks=int(input(“Enter mark:”))
data=[rollno,name,marks]
fout= open("Student.dat","wb")
pickle.________________ #Line 2
fout.close()
fin=___________________ #Line3
output=________________ #Line 4
fin.close()
if ____________>=33: #Line5
print(output[1]," passed”)
else:
print(output[1]," failed”)
a) csv
b) pickle
c) binary
d) bin
Answer : b
2. Fill in the blank in Line 2 with the function to write entire contents to file.
a) load(data,fout)
68
b) dump(data,fout)
c) writer(data)
d) insert(data,fout)
Answer : b
3. Fill in the blank in Line 3 to open the file for displaying contents of file.
a) open("Student.dat","w")
b) open("Student.dat","r")
c) open("Student.dat","wb")
d) open("Student.dat","rb")
Answer : d
a) pickle.read(fin)
b) pickle.readline(fin)
c) pickle.readlines(fin)
d) pickle.load(fin)
Answer: d
a) Output[0]
b) Output[1]
c) Output[2]
d) output
Answer : c
69
4 Ritesh wants to perform the following binary file operations, as a part of his
assignment, with the help of two user defined functions/modules:
import pickle
def AddEmp():
break
F.close()
def ViewEmp():
Total=0
Countrec=0
C50K=0
F=open("Employee.DAT","rb")
while True:
try:
C50K+=1
70
except:
break
if C50K==0:
AddEmp()
ViewEmp()
1. Write statement #Line1, to open the file “Employee.DAT” for writing only in
binary format?
a) F= open("Employee.DAT",'wb')
b) F= open("Employee.DAT",'w')
c) F= open("Employee.DAT",'wb+')
d) F= open("Employee.DAT",'w+')
Answer : a
2. Write statement, #Line2, to write the list L into the binary file,
Employee.DAT?
a) pickle.write(L,f)
b) pickle.write(f, L)
c) pickle.dump(L,F)
d) f=pickle.dump(L)
Answer : c
3. Write statement, #Line3, to read each record from the binary file
Employee.DAT?
a) R = pickle.load(F)
71
b) pickle.read(r,f)
c) r= pickle.read(f)
d) pickle.load(r,f)
Answer : a
4. Write statement , #Line4, to find employees who are getting salary more than
Rs.50000.
a) R[0]
b) R[1]
c) R[2]
d) R[3]
Answer : c
5. Write statement , #Line5, to find average salary of employees
a) Total/countrec
b) Total/C50K
c) Total/Countrec
d) average(Total)
Answer : c
72
b. ViewBook() to display the name, Author and price of books which are
more than Rs.350 in price. The function should also display the
average price of books.
Try to fill the incomplete code to get required information for librarian.
import pickle
def AddBook():
break
Fp.close()
def ViewBook():
Total=0
Count=0
C=0
F=open(________________) #Line3 to open file for
reading
while True:
try:
print(“Price of “,Row[1],"=
",Row[2])
C+=1
except:
break
if C==0:
avgprice=Total/Count
AddBook()
ViewBook()
1. Fill in the blank in Line1, to open the file “Books.DAT” for writing in binary
format?
a) Fp= open("Books.DAT",'w')
b) Fp= open("Books.DAT",'wb')
c) Fp= open("Books.DAT",'wb+')
d) Fp= open("Books.DAT",'w+')
Answer : b
2. Fill in the blank in Line2, to write the list Lst into the binary file, Books.DAT?
a) pickle.write(Lst,fp)
b) pickle.write(fp, Lst)
c) pickle.dump(Lst,Fp)
d) fp=pickle.dump(Lst)
Answer : c
3. Fill in the blank in Line3, to open the file “Books.DAT” for reading
a) F= open("Books.DAT",'r')
b) F= open("Books.DAT",'r+')
c) F= open("Books.DAT",'wb+')
d) F= open("Books.DAT",'rb')
Answer : d
c) Row=pickle.read(F)
d) Row=pickle.load(F)
Answer : d
5. Fill in the blank in Line5 with suitable expression:
a) Row[0]
b) Row[1]
c) Row[2]
d) Row[3]
Answer : c
75
def search():
if(_____________): #Line -4
print(rec)
except:
pass
_________________ # Line -5
1. Fill in the blank in Line1, to open the file “Competition.DAT” for reading in
binary format?
a) w
b) r
c) wb
d) rb
Answer : d
a) read(f)
b) readline(f)
c) load(f)
76
d) load()
Answer : c
Answer : b
5. Fill in the blank in Line5 to close the file pointer:
a) f.end()
b) f.close()
c) f=close()
d) close(f)
Answer : b
77
import pickle
def createFile():
fobj=_______________ #Line1 to open file for entering data to file
BookNo=int(input("Book Number : "))
Book_name=input("Name :")
Author = input(“Author: “)
Price = int(input("Price : "))
rec=[BookNo,Book_Name,Author,Price]
_________________ #Line2 to store data to file
fobj.close()
def CountRec(Author):
fobj=_______________ #Line3 to open file for searching
num= 0
try:
while True:
rec=_____________ # Line4 to read a record
if Author==______: #Line5 to check for specific author
num = num + 1
except:
fobj.close()
return num
1. Fill in the blank in Line1, to open the file “Books.DAT” for writing in binary
format?
a) open("Book.dat","ab")
b) open("Books.DAT",'wb')
c) open("Books.DAT",'a')
d) open("Books.DAT",'w+')
Answer : a
2. Fill in the blank in Line2, to write the list rec into the binary file, Books.DAT?
a) pickle.write(rec)
b) pickle.dump(rec,fobj)
c) pickle.dump(fobj,rec)
d) fobj=pickle.dump(rec)
Answer : b
3. Fill in the blank in Line3, to open the file “Books.DAT” for searching.
a) open("Book.dat","r")
b) open("Book.dat","rb")
c) open("Book.dat","wb")
d) open("Book.dat","ab”)
Answer : b
a) pickle.read(f)
b) pickle.load(f)
c) pickle.load(fobj)
d) pickle.read(fobj)
Answer : c
5. Fill in the blank in Line5 with suitable expression:
a) rec[0]
b) rec[1]
c) rec[2]
d) rec[3]
Answer : c
8 Neha, a software developer is asked to complete a search() function to search in
a pickled file Book.DAT.
Neha wants to complete the code and print details of Book with Book number
123.
def search():
______________: # Line -2
while True:
R = pickle.____________# Line -3
if(_____________): #Line -4
print(R)
except:
pass
_________________ # Line -5
1. Fill in the blank in Line1, to open the file “Book.DAT” for reading in binary
format?
a) w
b) r
c) wb
79
d) rb
Answer : d
2. Fill in the blank in Line2, to handle exceptions in statements
a) except
b) try
c) handle
d) statement
Answer : b
3. Fill in the blank in Line3, to read the records.
a) read(fp)
b) readline(fp)
c) load(fp)
d) load()
Answer : c
Answer : b
5. Fill in the blank in Line5 to close the file pointer:
a) fp.end()
b) fp.close()
c) fp=close()
d) close(fp)
Answer : b
def newadmission():
admnlst=[]
fobj=open(“Admission.dat”,”___”) #Line1 to open file for entering data to
file
while True:
AdmnNo=int(input("Admission Number : "))
Stud_name=input("Name :")
Fathername = input(“Father’s name: “)
Phone = int(input("Phone No : "))
rec=[AdmnNo,Stud_Name,Fathername,Phone]
admnlst.append(______) #Line2 to add a record
choice=input(“Enter more y/n”)
if choice in “Nn”:
break
_________________ #Line3 to store data to file
fobj.close()
def getRecords(AdmNo):
fobj=_______________ #Line4 to open file for searching
result=pickle.load(fobj)
for rec in result:
if rec[0]==______: #Line5 to check for given admission
number
print(rec)
fobj.close()
1. Identify missing code in Line1 so that file can add more information
a) w
b) r
c) wb
d) rb
Answer : c
a) pickle.dump(“admnlst”,”fobj”)
b) pickle.dump(admnlst,fobj)
c) pickle.dump(“fobj”,“admnlst”)
81
d) pickle.dump(fobj,admnlst)
Answer : b
Answer : d
5. Fill in the blank in Line5 to check for given admission number:
a) AdmnNo
b) “AdmnNo”
c) AdmNo
d) “AdmNo”
Answer : c
10 I. Ananya, a class 12 student is asked to write a binary file, “Fees.DAT”, for entering
fee details of students and review the details when required. She faced some
problems in statements and see if you can fill the missing code.
import pickle
def feeEntry():
Feelst=[]
fobj=open(“Fees.dat”,”___”) #Line1 to open file for entering data to file
while True:
AdmnNo=int(input("Admission Number : "))
Stud_name=input("Name :")
Class = input(“Enter Class: “)
Fee = int(input("Enter Fee : "))
rec=[AdmnNo,Stud_Name,Class,Fee]
Feelst.append(______) #Line2 to add a record
choice=input(“Enter more y/n”)
if choice in “Nn”:
break
_________________ #Line3 to store data to file
fobj.close()
def getRecords(AdmNo):
fobj=_______________ #Line4 to open file for searching
result=pickle.load(fobj)
for rec in result:
if rec[0]==______: #Line5 to check for given admission
number
print(rec)
fobj.close()
82
1. Identify missing code in Line1 so that file can add more information
a) w
b) r
c) wb
d) rb
Answer : c
Answer : d
5. Fill in the blank in Line5 to check for given admission number:
a) AdmnNo
b) “AdmnNo”
c) AdmNo
d) “AdmNo”
Answer : c
83
10 Mr ANAND NO2 CSV file: import csv module, open / close csv file,
GANESH GOLKONDA write into a csv file using csv.writerow() and read
from a csv file using csv.reader( )
1
The character that separates values in csv files is called the ………
a) delimit
b) delimiter
c) delimited
d) delimits
Ans: b)
2
The default delimiter of csv file is ……………
a) comma
b) colon
c) semicolon
d) hyphen
Ans:a)
3
The file mode to open a csv file for reading as well writing is ………
a) r
b) rw
c) r+
d) rb
Ans: c
84
4
The file mode to open a csv file for appending as well reading is ……..
a) w
b) w+
c) a
d) a+
Ans: d
5
To specify a different delimiter while writing into csv file, ……. argument is used with
csv.writer().
a) delimit
b) delimiter
c) delimited
d) delimits
Ans: b
6
To cancel the EOL translation in csv file while writing the data ………… argument is used
with open().
a) newline
b) next
c) open
d) EOL
Ans: a
7
To add data to an existing csv file, the mode of the file should be ……..
a) w
b) w+
c) a
d) a+
85
Ans: c
8
CSV stands for …….
Ans: b
9
Which module is used for working with CSV files in Python?
a) random
b) statistics
c) csv
d) math
Ans: c
10 Every record in a CSV file is stored in reader object in the form of a list using which
method?
a) writer()
b) append()
c) reader()
d) list()
Ans: c
program to read the CSV file “employee.csv” which will contain details of all the
employees. He has written the following code. As a programmer, help him to successfully
execute the given task.
import______ # Line 1
def readCsvEmp( ): # to read data from the CSV file
with ______('employees.csv', newline='') as f: # Line 2
2. csv import
3. import
4. export csv
2. open
3. close
4. append
Ans.2. open
(c) Fill in the blank in Line 3 to read the data from a csv file.
1. read
2. readline
3. reader
87
4. writer
Ans.3. reader
(d) Fill in the blank in Line 4 with the method to convert the data read from the file into list.
1. list
2. sets
3. dictionary
4. tuple
Ans.1. list
a. open("data.csv")
b. f=open("data.csv")
c. Both A & B are Correct
d. Both A & B are incorrect
Ans:a. open("data.csv")
a. load
b. read()
c. reader()
d. readlines()
Ans: c. reader()
4
Observe the following code and fill the blank in statement3
88
import csv
r = csv.______(f) #statement2
print(_____) #statement4
a. F
b. r
c. r,f
Ans: b. r
a. r
b. row
c. f
d. csv
Ans:b. row
6
Legend sports wanted to store the number of prizes for each sport as a SPORTS.CSV file.
fh=___________________ # Line 2
ans=‟y‟
i=1
89
while ans==‟y‟:
print(“Record”,i)
sport=input(“Sport name”)
__________ # Line 4
i=i+1
fh._________________#Line 5
1. .tsv
2. .csv
3. .py
4. .bin
Ans: 2. csv
1. fh = open(“sports.csv”,”w”)
2. read(“sports.csv”,”w”)
3. fh = file(“sports.csv”,”w”)
4. fh = append(“sports.csv”,”w”)
Ans: 1. fh = open(“sports.csv”,”w”)
90
c) Write the correct statement to write the data into file in line 3.
1. writerows( )
2. writerow( )
3. writer( )
4. swriter = csv.csvwriter(fh)
d) Write the statement to write the records given as input from user in line 4.
1. swriter([sport,prizes])
2. swriter.writrrow([sport,prizes])
3. swriter_writrrow([sport,prizes])
4. swriterwritrrow([sport,prizes])
Ans: 2. swriter.writrrow([sport,prizes])
7
Krishna of class 12 is writing a program to read the details of Sports
performance and store in the csv file “Sports.csv” delimited with a tab
f = open(“Sports.csv”,”a”)
ans = „y‟
91
i=1
print(“Record :”, i)
i += 1
f.___________ # Line 5
1. tcs
2. tmp
3. bin
4. csv
Ans : 4. Csv
1. open
2. writer
3. file
4. read
92
Ans : 2. writer
1. [prize,comp,sport]
2. [comp,prize,sport]
1. write
2. writerow
3. writeline
4. writelines
Ans: 2. writerow
8
Kumar is writing a program to create a CSV file “student.csv” which will contain rollno,
name and age of some students. He has written the following code. As a programmer,
help him to successfully execute the given task
f=open('student.csv','w',newline="")
p=csv._________(f) # Line 2
ch='y'
while ch=='y':
l=[]
93
rollno=int(input('enter rollno'))
name=input('enter name')
age=int(input('enter age'))
l.append(rollno)
l.append(name)
l.append(age)
p.___________(l) # Line 3
if ch=='y':
continue
else:
break
f.__________()
f=open('student.csv','r+')
c=list(csv.reader(f))
for i in c:
k=i[2]
if int(k)>15:
print(i)
f.close()
1. import csv
2. csv import
3. import
4. export csv
1. p=tsv.writer(f)
2. p=psv.writer(f)
3. p=csv.writer(f)
4. p=dsv.writer(f)
Ans : 3. p=csv.writer(f)
(c) The method which is to be used in line 3 to writes a row of data into the specified file
1. p.writerow(l)
2. p.writerows(l)
3 p.writer()
4. p.writerow(l)
Ans:4. p.writerow(l)
1. f.close()
2. f.open()
3. close.f()
4. f.read()
Ans: 1. f.close()
9
Puneeta is storing the data of her gift store in a csv file named gift.csv which will
store Gift_code and Gift_name of items of her gift store. She has written the
following code .As a programmer help her to successfully execute her program in
python:
def writeFile(Gift_code,Gift_name):
F=open(“gift.csv”,‟___‟) #Line 2
FW=csv.________(F) #Line 3
FW.writerow([Gift_code,Gift_name)
95
F.close()
def readFile():
FR=csv.reader(newF)
if row[0]==101:
print(row)
newF.close()
writeFile(101,”Photoframe”)
writeFile(102,”Soft Toys”)
writeFile(103,”Flower Pot”)
readFile() #Line 5
1. .tmp
2. .bin
3. .tsc
4. .csv
Ans: 4. csv
b) In which mode Puneeta should open the file to add data in it?
1. „a‟
2. „ab‟
3. „r‟
4. „w‟
Ans: 1. „a‟
1. close()
2. open()
3. writer()
4. append()
Ans: 3. writer()
d) Fill in the blank in Line 4 to open the csv file from the disk
1. close
2. open
3. write
4. read
Ans: 2. open
10
What is the output of the following program?
import csv
d=csv.reader(open('c:\PYPRG\ch13\city.csv'))
next(d)
for row in d:
print(row)
chennai,mylapore
mumbai,andheri
1. Chennai,mylapore
2. mumbai,andheri
3. chennai,mumba
4. chennai,mylapore mumbai,andheri
Ans: 2. mumbai,andheri
97
98
99
100
101
102
103
104
105
106
107
108
109