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

Answer Key Sample Paper 1

The code defines a function called FilterWords() that opens a text file called NewsLetter.TXT, reads each line, splits it into words, and prints any words with less than 4 characters. The student needs to fill in the blanks to make the code run properly by specifying the correct mode for opening the file

Uploaded by

Goutami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
559 views

Answer Key Sample Paper 1

The code defines a function called FilterWords() that opens a text file called NewsLetter.TXT, reads each line, splits it into words, and prints any words with less than 4 characters. The student needs to fill in the blanks to make the code run properly by specifying the correct mode for opening the file

Uploaded by

Goutami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

General Instructions:

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.

Do the needful with the following python code.

def FilterWords():

c=0

file=open('NewsLetter.TXT', '_____') #Statement-1

line = file._____ #Statement-2

word = _____ #Statement-3

for c in word:

if _____: #Statement-4

print(c) _________ #Statement-5

FilterWords()

i) Write mode of opening the file in statement-1?

a. a b. ab c. w d. r

ii) Fill in the blank in statement-2 to read the data from the file.

a. File.Read() b. file.read() c. read.lines( ) d. readlines( )

iii) Fill in the blank in statement-3 to read data word by word.

a. Line.Split() b. Line.split() c. line.split() d. split.word()

iv) Fill in the blank in statement-4, which display the word having lesser than 4 characters.

a. len(c) ==4 b. len(c) <4 c. len()= = 3 d. len()==3

v) Fill in the blank in Statement-5 to close the file.

a. file.close() b. File.Close() c. Close() d. end()

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 1


Q 2. Which of the following statements are true regarding the opening modes of a file?

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.

d. All of the above

Q 3. Which of the following commands can be used to read “n” number of characters from a file using the file
object <file>?

a. file.read(n) b. n=file.read() c. file.readline(n) d. file.readlines()

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>?

a. tmpfile.read(n) b. tmpfile.read() c. tmpfile.readline() d. tmpfile.readlines()

Q 5. What does the <readlines()> method returns?

a. Str b. a list of lines c. list of single characters d. list of integers

Q 6. What is the use of tell() method in python?

a. tells you the current position within the file

b. tells you the end position within the file

c. tells you the file is opened or not

d. None of the above

Q 7. Which of the following functions do you use to write data in the binary format?

a. write b. output c.dump d. send

Q 8. Which of the following functions do you use to read data in the binary format?

a. load b. input c. dump d. read

Q 9. What would be the output of the following :

>>> 25<45<26

a. False b. True c. answer may vary d. 45

Q 10. What will be the output of the following code snippet?

a=[1,2,3,4,5]

print(a[4:0:-1])

a. Syntax error b. [4,3,2] c. [5,4,3,2] d. [4,3,2,1]

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 2


Q 11. What will be the output of the following?

a=[1,2,3,4,5,6,7,8,9]

print(a[::3])

a. [1,4,7] b. [8,9] c. [1,3,5,7,9] d. [1,2,3]

Q 12. Suppose t=[1,2,4,3,8,9]

What will be the output?

>>> for i in range(0, len(t),2):

Print(t[i])

a. [2,3,9] b. [1,2,4,3,8,9] c. [1,4,8] d.(1,4,8)

Q 13. The ______method remove all items from the dictionary.

a. Pop() b. del() c. clear() d. all

Q 14. Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is incorrect?

a. print(T[1]) b. T[2] = -29 c. print(max(T)) d. print(len(T))

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 19. Import all objects from a module as :

a. Import all b. import <Module_name>* c. import * d. from<Module_name> import *

Q 20. Collection of modules in a single unit is known as _______.

a. User-defined modules b. Built-in Modules c. Package d. Functions

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 3


Q 21. The _________are useful for documentation purposes.

a. docstrings b. statements c. comments d. help()

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.

Code: pay= _________

location= _________

if location == "Mumbai":

print ("I’ll take it!") #Statement 1

elif location == "Chennai":

if pay < 100000:

print ("No way") #Statement 2

else:

print("I am willing!") #Statement 3

elif location == "Delhi" and pay > 40000:

print("I am happy to join") #Statement 4

elif pay > 60000:

print("I accept the offer") #Statement 5

else:

print("No thanks, I can find something better") #Statement 6

On the basis of the above code, choose the right statement which will be executed when different inputs for
pay and location are given.

i) Input: location = "Chennai”, pay = 50000

a. Statement 1 b. Statement 2 c. Statement 3 d. Statement 4

ii) Input: location = "Surat" ,pay = 50000

a. Statement 2 b. Statement 4 c. Statement 5 d. Statement 6

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 4


iii) Input- location = "Any Other City", pay = 1

a. Statement 1 b. Statement 2 c. Statement 4 d. Statement 6

iv) Input location = "Delhi", pay = 500000

a. Statement 6 b. Statement 5 c. Statement 4 d. Statement 3

v) Input- location = "Lucknow", pay = 65000

a. Statement 2 b. Statement 3 c. Statement 4 d. Statement 5

Q 24. Consider the following code and answer the questions that follow:

Book={1:'Thriller', 2:'Mystery', 3:'Crime', 4:'Children Stories'}

Library ={'5':'Madras Diaries','6':'Malgudi Days'}

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:

a. Book[2]=’Crime Thriller’ b. Book[3]=’Crime Thriller’ c. Book[2]=(’Crime Thriller’)


d. Book[3] =(‘Crime Thriller’)

ii) The command to merge the dictionary Book with Library the command would be:

a. d=Book+Library b. print(Book+Library) c. Book.update(Library)


d. Library.update(Book)

iii) What will be the output of the following line of code:

print(list(Library))

a. [‘5’,’Madras Diaries’,’6’,’Malgudi Days’] b. (‘5’,’Madras Diaries’,’6’,’Malgudi Days’)


c. [’Madras Diaries’,’Malgudi Days’] d. [‘5’,’6’]

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:

‘Madras Diaries’ in Library

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.

b. We must use the function Library.values() along with the in operator

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 5


c. We can use the Library.items() function instead of the in operator

d. Both b and c above are correct.

Q 25. What is the output of the following code?

def foo(k):

k=[1]

q=[0]

foo(q)

print(q)

a. [0] b. [1] c. [1,0] d. [0,1]

Q 26. The output of the code shown below is :

def f1():

x=15

print(x)

x=12

f1()

a. Error b. 12 c. 15 d. 1512

Q 27. Select the option that print

hello-how-are-you

a. print(‘hello’,’how’,’are’,’you’)

b. print(‘hello’,’how’,’are’,’you’ + ‘-‘ * 4)

c. print(‘hello-‘ + ‘how-are-you’)

d. print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

Q 28. What is the output of the following ?

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)

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 6


a. (‘check’, ‘check’,’check’) b. * operator not valid for tuples c. ‘checkcheckcheck’
d. Syntax error

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

fh = open(_____, _____, newline='') #Statement-2

stuwriter = csv._____ #Statement-3

data = []

header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']

data.append(header)

for i in range(5):

roll_no = int(input("Enter Roll Number : "))

name = input("Enter Name : ")

Class = input("Enter Class : ")

section = input("Enter Section : ")

rec = [_____] #Statement-4

data.append(rec)

stuwriter. _____ (data) #Statement-5

fh.close()

i) Identify the suitable code for blank space in line marked as Statement-1.

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 7


a. csv file b. CSV c. csv d. Csv

ii) Identify the missing code for blank space in line marked as Statement-2?

a. "School.csv","w" b. "Student.csv","w" c. "Student.csv","r" d. "School.csv","r"

iii) Choose the function name (with argument) that should be used in the blank space of line marked as
Statement-3

a. reader(fh) b. reader(MyFile) c. writer(fh) d. writer(MyFile)

iv) Identify the suitable code for blank space in line marked as Statement-4.

a. 'ROLL_NO', 'NAME', 'CLASS', 'SECTION' b. ROLL_NO, NAME, CLASS, SECTION


c. 'roll_no','name','Class','section' d. roll_no,name,Class,sectionc) co.connect()

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?

a. dump() b. load() c. writerows() d. writerow()

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”)

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 8


myfile.read()

b. myfile = open(“poem.txt”,”r”)

myfile.read(100)

Ans : a. This will read the entire file “poem.txt”

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

Q 3. What will be the output of the following code snippet? 1x2=2

i) f0=open(“myfile.txt”,”w+”)

print(“Name of the file : “,f0.name)

seq = “Computer Science\n Python Programming”

f0.writelines(seq)

f0.seek(0,0)

for line in f0:

print(line)

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 9


f0.close()

Ans : Name of the file : myfile.txt

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

Ans : for line in file(“poem.txt”):

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:

f.write(“Hello world how are you today”)

with open (“hello.txt”,’r’) as f:

data = f.readlines()

for line in data:

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])

v) What is the output of the following code? 1x1=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

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 10


Q 5. Write a program to replace capital letters with their respective small letters in the same file. 1x5=5

Ans : def replace():

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()

Q 6. Write a program to search a record in a binary file. 1x5=5

Ans : def search_record():

f=open(“student_details.dat”,”rb”)

data=pickle.load()

f=int(input(“Enter the roll number which you want to search”))

for i in data:

if i[0]==r:

print(“Record Found….”)

print(i)

found=1

break

else :

print(“Record not found”)

XII/COMPUTER SCIENCE/TERM I/2021-22 Page 11

You might also like