XII - CS Questions
XII - CS Questions
dictionary D1?
a. D1.add(key = “Red”, value = “courage”) c. D1[Red] = “Courage”
b. D1.add(“Red”, “courage”) d. D1[“Red”] = “Courage”
Consider the statements given below and then choose the correct output from the given options:
Motto = "Be Prepared"
7. print(Motto[-1:0:-3]) 1
a. erad b. dare c. deep d. BPpe
Which of the following will remove the value 34 from the list L = [12, 23, 34, 45, 34]?
8. 1
a. del L[34] b. L.pop(34) c. L.remove(34) d. L.del(34)
Which of the following statement(s) would give an error during execution of the following
code?
L = [10, 20, 30, [1, 2], "python", (100, 10)]
print(L) # Statement 1
9. print(L + L[3]) # Statement 2 1
print(L[5] + L[0]) # Statement 3
L[5][0] = 50 # Statement 4
a. Statement 1 c. Statement 2 & 3
b. Statement 3 & 4 d. Statement 4
What possible outputs(s) will be obtained when the following code is executed?
import random
n1 = random.randint(0, 1)
n2 = random.randint(2, 3)
city = ["Mumbai", "Chennai","Kolkatta", "Patna"]
10. for i in range(n1, n2): 1
print(city[i+1].upper(), end = "&")
a. MUMBAI&CHENNAI& c. CHENNAI&KOLKATTA&PATNA&MUMBAI&
b. CHENNAI&KOLKATTA& d. MUMBAI&CHENNAI&KOLKATTA&
Consider the code given below:
a = 10
def change():
global b
b = a
11. b += a 1
print(a, end = "#")
change()
print(a)
What possible output will be obtained when the above code is executed?
a. 10#20 b. 10 #10 c. 20#10 d. Error
12. Consider the code given below: 1
a = 40
L = [10, 40, 50]
def find():
___________ # missing statement
a %= 3
L.append(a)
3
find()
L.insert(a, 100)
print(L)
Which of the following statements should be given in the blank for #Missing Statement, if the
output produced is [10, 100, 40, 50, 1]?
a. global L b. global a c. global a = 1 d. global a %= 3
State whether the following statement is True or False:
13. 1
Python raises ValueError if the name is not found.
Which of the following statements is FALSE about a Stack.
a. Stack is a Linear Data Structure.
14. b. Insertion and deletion happens at one end, that is stack’s top. 1
c. Adding a new element to a stack is called PUSH operation.
d. Removing an element from the top of the stack is known as PEEK.
Select the correct statement to move the file pointer 20 bytes ahead of the current position of the
15. file pointer if the file handle is fh. 1
a. fh.seek(20, 1) b. fh.seek(1, 20) c. fh.seek(-20, 1) d. fh.seek(20, -1)
Which of the following functions displays the position of the file pointer?
16. 1
a. flush() b. tell() c. seek() d. stdout()
Q17 and 18 are ASSERTION AND REASONING based questions. Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
Assertion (A): Dictionary is an immutable data type in Python.
17. Reasoning (R): Dictionary is a unordered collection with elements in the form of a key:value 1
pairs that associate keys to value. Keys of the dictionary must be immutable type.
Assertion (A): The string replication operator * is used with a string and a number.
Reasoning (R): The * operator when used with the numbers (i.e., when both operands are
18. numbers), it performs the multiplication and when used with a string and a number (i.e., as 1
number * string or string * number) Python creates a new string that is a number of repetitions
of the given string.
SECTION B
19. Differentiate between implicit type conversion and explicit type conversion. 2
The code given below accepts a list as an argument and returns the mean of a given list of
numbers. Observe the following code carefully and rewrite it after removing all syntax and
logical errors. Underline all the corrections made.
Def MEAN(L):
mean == sum == 0
for i in (0, len(L)):
20. 2
sum += L[i]
mean = sum / len(L)
return mean
L = [12, 10, 14, 9, 15]
print("The given list is ", L)
print("Mean : ", MEAN(L))
21. Write a function displayWords(Names) in Python, that takes the dictionary, Names as an 2
4
argument and displays the names (in uppercase) of the names whose last character is ‘a’.
For example, Consider the following dictionary
Names = {101:"Camila", 102:"Sara", 103:"Indhu", 104:"Bhaskar"}
The output should be:
CAMILA
SARA
OR
Write a function, findWords(STR), that takes a string as an argument and prints the words of
each character separated by # of those words whose length is more than 4 characters.
Write a the Python statement for each of the following tasks using BUILT-IN functions/methods
only:
23. (i) To delete an element from the third position of the list L1. 1+1
(ii) To check whether the string named, STR contains only upper cased letters or not.
Predict the output of the following code:
def create(Str):
i = len(Str)-1
S = ''
while i >= 0 :
if Str[i].isupper():
S = S + Str[i].join("@@").lower()
24. elif Str[i].islower(): 2
S = S + Str[i].upper()
i -= 1
print(S)
S = "Stay PosiTive"
create(S)
25. Predict the output of the following code: 2
5
Each of these records are nested together to form a nested list. Write the following user defined
functions in Python to perform the specified operations on the stack named MOONS.
(i) PushMOONS(): To push a list object containing Name of the Planet and No. of
Moons which has Moons.
(ii) PopMOONS(): To pop the objects from the stack and display them. Also, the
function should display “No more Planets!” when there are no elements in the stack.
Write a user defined function, findCost(FURN), that accepts the dictionary FURN as
parameter and displays all the records from the binary file FURNITURE.DAT, that have the
value of FTYPE as Table and COST more than 10000.
33. Amritya Seth is a programmer, who has recently been given a task to write a python code to 5
perform the following binary file operations with the help of two user defined
functions/modules:
a. AddStudents() to create a binary file called STUDENT.DAT containing student information
– roll number, name and marks (out of 100) of each student.
b. GetStudents() to display the name and percentage of those students who have a percentage
greater than 75. In case there is no student having percentage > 75 the function displays a
message as “No Student scored more than 75”. The function should also display the average
percent.
He has succeeded in writing partial code and has missed out certain statements, so he has left
certain queries in comment lines. You as an expert of Python have to provide the missing
statements and other related queries based on the following code of Amritya.
import pickle
def AddStudents():
____________ #1 statement to open the binary file to write data
while True:
Rno = int(input("Rno :"))
Name = input("Name : ")
Percent = float(input("Percent :"))
L = [Rno, Name, Percent]
____________ #2 statement to write the list L into the file
Choice = input("enter more (y/n): ")
if Choice in "nN":
break
F.close()
def GetStudents():
Total=0
Countrec=0
Countabove75=0
with open("STUDENT.DAT","rb") as F:
while True:
try:
____________ #3 statement to read from the file
8
Countrec+=1
Total+=R[2]
if R[2] > 75:
print(R[1], " has percent =",R[2])
Countabove75+=1
except:
break
if Countabove75==0:
print("There is no student who has percentage more than 75")
average = Total/Countrec
print("average percent of class = ",average)
AddStudents()
GetStudents()
Which of the following commands is used to open the file “STUDENT.DAT” for writing only
in binary format? (marked as #1 in the Python code)
a. F= open("STUDENT.DAT",'wb')
a)
b. F= open("STUDENT.DAT",'w')
c. F= open("STUDENT.DAT",'wb+')
d. F= open("STUDENT.DAT",'w+')
Which of the following commands is used to write the list L into the binary file,
STUDENT.DAT? (marked as #2 in the Python code)
b)
a. pickle.write(L,f) b. pickle.write(f, L)
c. pickle.dump(L,F) d. f=pickle.dump(L)
Which of the following commands is used to read each record from the binary file
c) STUDENT.DAT? (marked as #3 in the Python code)
a. R = pickle.load(F) b. pickle.read(r,f) c. r= pickle.read(f) d. pickle.load(r,f)
Which of the following statement(s) are correct regarding the file access modes?
a. ‘r+’ opens a file for both reading and writing. File object points to its beginning.
b. ‘w+’ opens a file for both writing and reading. Adds at the end of the existing file if it exists
d) and creates a new one if it does not exist.
c. ‘wb’ opens a file for reading and writing in binary format. Overwrites the file if it exists and
creates a new one if it does not exist.
d. ‘a’ opens a file for appending. The file pointer is at the start of the file if the file exists.
Which of the following statements correctly explain the function of seek() method?
a. tells the current position within the file.
e) b. determines if you can move the file position or not.
c. indicates that the next read or write occurs from that position in a file.
d. moves the current file position to a given specified position
SECTION E
34. Radha Shah is a programmer, who has recently been given a task to write a python code to 4
perform the following CSV file operations with the help of two user defined functions/modules:
a. CSVOpen() : to create a CSV file called BOOKS.CSV in append mode containing
information of books – Title, Author and Price.
b. CSVRead() : to display the records from the CSV file called BOOKS.CSV where the
field title starts with 'R'.
She has succeeded in writing partial code and has missed out certain statements, so she has left
certain queries in comment lines.
9
import csv
def CSVOpen():
with open('books.csv','______',newline='')as csvf: #Statement-1
cw=______ #Statement-2
______ #Statement-3
cw.writerow(['Rapunzel','Jack',300])
cw.writerow(['Barbie','Doll',900])
cw.writerow(['Johnny','Jane',280])
def CSVRead():
try:
with open('books.csv','r') as csvf:
cr=______ #Statement-4
for r in cr:
if ______: #Statement-5
print(r)
except:
print('File Not Found')
CSVOpen()
CSVRead()
You as an expert of Python have to provide the missing statements and other related queries
based on the following code of Radha. Answer any four questions (out of five) from the below
mentioned questions.
Choose the appropriate mode in which the file is to be opened in append mode (Statement 1)
a)
a. w+ b. ab c. r+ d. a
Which statement will be used to create a csv writer object in Statement 2.
b)
a. csv.writer(csvf) b. csv.writer(csvf) c. csvf.writer() d. cs.writer(csvf)
Choose the correct option for Statement 3 to write the names of the column headings in the CSV
file, BOOKS.CSV.
c)
a. cw.writerow('Title','Author','Price') b. cw.writerow(['Title','Author','Price'])
c. cw.writerows('Title','Author','Price') d. cw.writerows(['Title','Author','Price'])
Which statement will be used to read a csv file in Statement 4.
d)
a. cs.read(csvf) b. csv.reader(csvf) c. csvf.read() d. csvf.reader(cs)
Fill in the appropriate statement to check the field Title starting with ‘R’ for Statement 5 in the
e) above program.
a. r[0][0]=='R' b. r[1][0]=='R' c. r[0][1]=='R d. r[1][1]=='R'
35. Shreyas is a programmer, who has recently been given a task to write a user defined function 4
named write_bin() to create a binary file called cust_file.dat containing customer
information – customer number (c_no), name (c_name), quantity(qty), price (price) and
amount (amt) of each customer.
The function accepts customer number, name, quantity and price. Thereafter, it displays the
message “Quantity less than 10…. Cannot SAVE”, if quantity entered is less than 10. Otherwise
the function calculates amount as price * quantity and then writes the record in the form
of a list into the binary file.
import pickle
10
def write_bin():
bin_file = __________ # Statement 1
while True:
c_no = int(input("Enter customer number"))
c_name = input("Enter customer name")
qty = int(input("Enter qty"))
price = int(input("Enter price"))
if _________ # Statement 2
print("Quantity less than 10...Cannot SAVE")
else:
amt = price * qty
c_detail = [c_no, c_name, qty, price, amt]
_________ # Statement 3
ans = input("Do you wish to enter more records y/n")
if ans.lower() == 'n':
________ # Statement 4
____________________ # Statement 5
________________________ # Statement 6
(i) Write the correct statement to open a file “cust_file.dat” for writing the data
of the customer.
(ii) Which statement should Shreyas fill in Statement 2 to check whether quantity is less
than 10.
(iii) Which statement should Shreyas fill in Statement 3 to write data to the binary file
and in Statement 4 to stop further processing if the user does not wish to enter more
records.
(OR)
(Option for part (iii) only)
(iv) What should Shreyas fill in Statement 5 to close the binary file named
cust_file.dat and in Statement 6 to call a function to write data in binary file?
**************