Answer Key Sample Paper 1
Answer Key Sample Paper 1
1. This question paper contains two parts A and B. Each part is compulsory.
2. Part-A has 50 Multiple Choice Questions carries 1 mark each.
3. Part - B is Descriptive Paper.
4. Part- B has three sections
a. Section-I is short answer questions of 2 marks each in which two question have internal options.
b. Section-II is long answer questions of 3 marks each in which two questions have internal options.
c. Section-III is very long answer questions
PARTof-A 5 marks each in which two questions have internal option.
Q1. Your teacher has given you a method/function FilterWords() in python which read lines from a text file
NewsLetter.TXT, and display those words, which are lesser than 4 characters. 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.
def FilterWords():
c=0
for c in word:
if _____: #Statement-4
FilterWords()
a. a b. ab c. w d. r
ii) Fill in the blank in statement-2 to read the data from the file.
iv) Fill in the blank in statement-4, which display the word having lesser than 4 characters.
a. When you open a file for reading, if the 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.
Q 3. Which of the following commands can be used to read “n” number of characters from a file using the file
object <file>?
Q 4. Which of the following commands can be used to read the entire contents of a file as a string using the
file object <tmpfile>?
Q 7. Which of the following functions do you use to write data in the binary format?
Q 8. Which of the following functions do you use to read data in the binary format?
>>> 25<45<26
a=[1,2,3,4,5]
print(a[4:0:-1])
a=[1,2,3,4,5,6,7,8,9]
print(a[::3])
Print(t[i])
Q 14. Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is incorrect?
Q 15. Anshuman wants to separate the values by a $ sign. Suggest to him a pair of function and parameter to
use it.
a. pen,quotechar b. writer,quotechar c. open,delimiter d. writer, delimiter
Q 16. Out of the following find the identifiers, which cannot be used for naming variable or Functions in a
Python program : _Cose, Price*Qty, float, switch, Address one, Delete, Number12, for
a. _Cost, Price*Qty, switch, Delete b. Price*Qty, float, Address one, for
c.Price*Qty, Number12, Address one d. _Cost, switch, Delete , Number12
Q 17. Values provided in function call _________ and values provided in function header ___________.
a. Values and Arguments b. Arguments and Parameters c. Parameter and
Arguments d. Parameters and Values
Q 18. If there is a function with many parameters and we want to specify only some of them in function call,
then value for such parameters can be provided by using their names instead of the positions is known as
_________.
a. Default Arguments b. Positional Arguments c. Keyword Arguments
d. User-Defined Arguments
Q 22. Which of the following parameter needs to be added with open function to avoid blank row followed file
each row in CSV file?
a. qoutechar b. quoting c. newline d. skiprow
Q 23. Krrishnav is looking for his dream job but has some restrictions. He loves Delhi and would take a job
there if he is paid over Rs.40,000 a month. He hates Chennai and demands at least Rs. 1,00,000 to work there.
In any another location he is willing to work for Rs. 60,000 a month. The following code shows his basic
strategy for evaluating a job offer.
location= _________
if location == "Mumbai":
else:
else:
On the basis of the above code, choose the right statement which will be executed when different inputs for
pay and location are given.
Q 24. Consider the following code and answer the questions that follow:
i) Ramesh needs to change the title in the dictionary book from ‘Crime’ to ‘Crime Thriller’. He has
written the following command:
Book[‘Crime’]=’Crime Thriller’
But he is not getting the answer. Help him choose the correct command:
ii) The command to merge the dictionary Book with Library the command would be:
print(list(Library))
iv) In order to check whether the key 2 is present in the dictionary Book, Ramesh uses the following
command:
2 in Book
He gets the answer ‘True’. Now to check whether the name ‘Madras Diaries’ exists in the dictionary
Library, he uses the following command:
But he gets the answer as ‘False’. Select the correct reason for this:
a. We cannot use the in function with values. It can be used with keys only.
def foo(k):
k=[1]
q=[0]
foo(q)
print(q)
def f1():
x=15
print(x)
x=12
f1()
a. Error b. 12 c. 15 d. 1512
hello-how-are-you
a. print(‘hello’,’how’,’are’,’you’)
b. print(‘hello’,’how’,’are’,’you’ + ‘-‘ * 4)
c. print(‘hello-‘ + ‘how-are-you’)
x = [‘ab’, ‘cd’]
for i in x:
x. append(i.upper())
print(x)
a. [‘AB’, ‘CD’] b. [‘ab’, ‘cd’, ‘AB’, ‘CD’] c. [‘ab’, ‘cd’] d. None of the mentioned
Q 29. What is the output of the following piece of code when executed?
a = (“check”) * 3
Print (a)
Q 30. Which of the following file types allows to store large data files in the computer memory?
a. Text Files b. Binary Files c. CSV Files d. None of these
Q 31. Rohit, a student of class 12th, is learning CSV File Module in Python. During examination, he has been
assigned an incomplete python code (shown below) to create a CSV File 'Student.csv' (content shown below).
Help him in completing the code which creates the desired CSV File.
CSV File
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A
Incomplete Code
import_____ #Statement-1
data = []
data.append(header)
for i in range(5):
data.append(rec)
fh.close()
i) Identify the suitable code for blank space in line marked as Statement-1.
ii) Identify the missing code for blank space in line marked as Statement-2?
iii) Choose the function name (with argument) that should be used in the blank space of line marked as
Statement-3
iv) Identify the suitable code for blank space in line marked as Statement-4.
v) Choose the function name that should be used in the blank space of line marked as Statement-5 to create
the desired CSV File?
Q 32. To read 4th line from text file, which of the following statement is true?
a. dt = f.readlines();print(dt[3]) b. dt=f.read(4) ;print(dt[3]) c. dt=f.readline(4);print(dt[3])
d. All of these
Q 33. Which of the following functions flushes the data before closing the file?
a. flush() b. close() c. open() d. fflush()
Q 34. 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’
Q 35. 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. Which of the following can be the correct statement
for her?
a. f = open(‘sum_list’,’wb’); pickle.dump(l1,f) b. f = open(‘sum_list’,’rb’); l1=pickle.dump(f)
c. f = open(‘sum_list’,’wb’); pickle.load(l1,f) d. f = open(‘sum_list’,’rb’); l1=pickle.load(f)
PART B
Section - I
Q 1. How are the following codes different form one another? 1x2=2
a. myfile = open(“poem.txt”,”r”)
b. myfile = open(“poem.txt”,”r”)
myfile.read(100)
b.This will read at most 100 bytes for the file even if the file contains more data than that.
Q 2. Read the code given below and answer the question: 2x1=2
i) fh=open(“main.txt”,”w”)
fh.write(“Bye”)
fh.close()
If the file contains “GOOD” before execution, what will be the contents of the file after the execution of this
code?
Ans : This file would now contain “Bye” only, because when an existing file is opened in write mode(“w”), it
truncates the existing data file.
ii) Consider the following and predict the output of the following code fragment.
f=open(“poem.txt”,”r”)
n1=0
for line in f:
n1=n1+1
print(n1)
Ans : It returns the total number of lines present in the file “poem.txt”.
Section - II
i) f0=open(“myfile.txt”,”w+”)
f0.writelines(seq)
f0.seek(0,0)
print(line)
Computer Science
Python Programming
ii) Write a single loop to display all the contents of a text file “poem.txt” after removing leading and
trailing whitespaces. 1x1=1
Print(line.strip())
iii) Q 4. I) What will be the output of the following code snippet? 1x1=1
With open(“hello.txt”,”w”) as f:
data = f.readlines()
words=line.split()
print(words)
f.close()
Ans : [‘Hello’,’world’,’how’,’are’,’you’,’today’]
iv) ii) Write a code to print the last line of a text file. 1x1=1
Ans : fin=open(“data.txt”,”r”)
lst = fin.readlines()
print(lst[-1])
f=open(“poem.txt”,”r”)
size=len(f.read())
print(f.read(5))
Ans : No output, f.read() of line 2 will read the entire file content and place the file pointer at the end of the
file. So f.read(5) returns nothing as there are no more bytes to be read. So print() statement prints nothing.
Section - III
f=open(“poem.txt”,”r+”)
data=f.read()
f.seek(0)
for i in data :
if i.isupper():
f.write(i.lower())
else :
f.write(i)
f.close()
replace()
f=open(“student_details.dat”,”rb”)
data=pickle.load()
for i in data:
if i[0]==r:
print(“Record Found….”)
print(i)
found=1
break
else :