ComputerScience SQP Set2 MS
ComputerScience SQP Set2 MS
SAMPLE PAPER - II
SECTION - A
1. The statement is an empty statement in Python. 1
Ans: pass
2. Which of the following is not a keyword? 1
(a) eval (b) assert (c) nonlocal (d) pass
Ans: a
3. What will be the output for the following Python statements? 1
D= {“Amit”:90, “Reshma”:96, “Suhail”:92, “John”:95}
print(“John” in D, 90 in D, sep= “#”)
(a) True#False (b)True#True (c) False#True (d)
False#False
Ans: a
4. What will the following Python statement evaluate to? 1
print (5 + 3 ** 2 / 2)
(a) 32 (b) 8.0 (c) 9.5 (d) 32.0
Ans: c
5. Consider the list aList=[ “SIPO”, [1,3,5,7]]. What would the following code print? 1
print(aList[0][1],aList[1][1],sep=',')
(a) S, 3 (b) S, 1 (c) I, 3 (d) I, 1
Ans: c
6. Which of the following mode in the file opening statement creates a new file if the file does not exist? 1
(a) r+ (b) w+ (c) a+ (d) Both (b) and (c)
Ans: d
7. Which of the following is not an integrity constraint? 1
(a) Not null (b) Composite key (c) Primary key (d) Check
Ans: b
8. Choose the correct command to delete an attribute A from a relation R. 1
(a) ALTER TABLE R DELETE A (b) ALTER TABLE R DROP A
(c) ALTER TABLE DROP A FROM R (d) DELETE A FROM R
Ans: b
9. Identify the errors in the following code: 1
MyTuple1=(1, 2, 3) #Statement1
MyTuple2=(4) #Statement2
MyTuple1.append(4) #Statement3
print(MyTuple1, MyTuple2) #Statement4
(a) Statement 1 (b) Statement 2 (c) Statement 3 (d) Statement 2 &3
Ans: d
10. In the relational model, relationships among relations/table are created by using keys. 1
(a) composite (b) alternate (c) candidate (d) foreign
Ans: d
11. The correct statement to place the file handle fp1 to the 10th byte from the current position is: 1
(a) fp1.seek(10) (b) fp1.seek(10, 0) (c) fp1.seek(10, 1) (d) fp1.seek(10, 2)
Ans: c
12. Which of the following aggregate functions ignore NULL values? 1
(a) COUNT (b) MAX (c) AVERAGE (d) All of these
Ans: d
13. is a device that forwards data packets along networks. 1
(a) Gateway (b) Modem (c) Router (d) Switch
Page 1 of 12
Ans: c
14. Suppose str= ‘welcome’. All the following expression produce the same result except one. Which one? 1
(a) str[ : : -6] (b) str[ : : -1][ : : -6] (c) str[ : :6] (d) str[0] + str[-1]
Ans: a
15. Which of the following is a DML command? 1
(a) SELECT (b) ALTER (c) CREATE (d) DROP
Ans: (a)
16. Mandatory arguments required to connect any database from Python are: 1
(a) Username, Password, Hostname, Database name, Port
(b) Username, Password, Hostname
(c) Username, Password, Hostname, Database Name
(d) Username, Password, Hostname, Port
Ans: (b) -when create database (c) – when working in anyone database
17 What is the output of the below program? 1
def printMax(a, b):
if a > b:
print(a, ‘is maximum’)
elif a == b:
print(a, ‘is equal to’, b)
else:
print(b, ‘is maximum’)
printMax(3, 4)
a) 3 b) 4 c) 4 is maximum d) None of the mentioned
Ans:(c)
18 What is the use of tell() method in Python? 1
(a) returns the current position of record pointer within the file
(b) returns the end position of record pointer within the file
(c) returns the current position of record pointer within the line
(d) none of the above
Ans: (a)
19 Ms. Suman is working on a binary file and wants to write data from a list to a binary file. Consider list 1
object as L1, binary file sum_list.dat, and file object as f. Which of the following can be the correct
statement for her?
a) f = open(‘sum_list.dat’,’wb’); b) f = open(‘sum_list.dat’,’rb’);
pickle. dump(L1,f) L1=pickle.dump(f)
c) f = open(‘sum_list.dat’,’wb’); d) f = open(‘sum_list.dat’,’rb’);
pickle.load(L1,f) L1=pickle.load(f)
Ans: (c)
Q20 and 21 are ASSERTION and REASONING based questions. Mark the correctchoice as: 1
(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
20 Assertion (A): Keyword arguments are related to the function calls 1
Reason (R): When you use keyword arguments in a function call, the caller identifies the arguments by
the parameter name.
Ans: (A)
21 Assertion (A): Text file stores information in ASCII or unicode characters. 1
Reason (R): In text file, there is no delimiter for a line.
Ans: (c)
SECTION-B
Page 2 of 12
22 Identify the errors in the program segment given below. Rewrite the corrected codeand underline each 2
correction.
Page 3 of 12
Ans : ZeroDivisionError
finally
25 What do you understand by Candidate keys in a table? Give a suitable example of candidate keys from a 2
table containing some meaningful data.
All attributes or group of attributes of a table that can identify a tuple uniquely is called a candidate key.
(1 mark)
ITEM
Ino Item Qt
I01 Pen 565
I02 Pencil 780
I03 CD 450
I04 Eraser 300
I05 Duster 200
Candidate keys-Ino,Item
26 (i) Write the full forms of the following: 2
(a) SMTP (b) IPR
(ii) Which protocol is used in creating a connection with a remote machine?
Ans: (i) SMTP – Simple Mail Transfer Protocol IPR – Intellectual Property Rights
(iii) TELNET
27 What possible output(s) are expected to be displayed on screen at the time of executionof the following 2
code? Also specify the maximum and minimum value that can be assigned to variable X.
import random
L=[10,7,21]
X=random.randint(1,2)
for i in range(X):
Y=random.randint(1,X)
print(L[Y],”$”,end=” ”)
(a) 10 $ 7 $ (b) 21 $ 7 $ (c) 21 $ 10 $ (d) 7 $
Ans: (b) and (d)
Maximum Value of X: 2
Minimum Value of X=1
OR
def changer(p, q=10):
p=p/q
q=p%q
print(p,’#’,q)
return p
a=200
b=20
a=changer(a,b)
print(a,’$’,b)
a=changer(a)
print(a,’$’,b)
Ans: 10.0 # 10.0
10.0 $ 20
1.0 # 1.0
Page 4 of 12
1.0 $ 20
28 Differentiate between Drop and Delete commands of MYSQL. Write its syntax. 2
Delete – Drop –
Data Manipulation Language (DML) command and Data Definition Language (DDL) command
used when you want to remove some orall the which removes the table from database
tuples from a relation Syntax:
Syntax: Drop table tablename;
Delete: DELETE FROM relation_nameWHERE
condition;
OR
For a table “Company” having column cno, cname, department and salary, write the SQLstatement to
display average salary of each department.
Ans: SELECT department, avg(salary) from company Group by department;
SECTION – C
29 A text file “Quotes .Txt” has the following data written in it: 3
Living a life you can be proud of Doing your best Spending your time with people and activities that are
important to you Standing up for things that are right even when it’s hard Becoming the best version of
you.
Write a user defined function to display the total number of words present in the file.
Ans:
def countwords():
s = open("Quotes.txt","r")
f = s.read()
words = f.split()
count = 0
for word in words:
count = count + 1
print ("Total number of words:", count)
f.close( )
OR
Write a function in Python to count the number of lines in a text file ‘STORY.TXT’ which is starting
with an alphabet ‘A’.
Ans:
def countwords():
s = open("Story.txt","r")
lines= s.readlines()
count = 0
for line in linse:
if line[0] in ‘Aa’:
count = count + 1
print ("Total number of lines:", count)
f.close( )
30 A list contains following record of course details for a University: 3
[Course_name, Fees, Duration]
Write the following user defined functions to perform given operations on the stack named 'Univ':
(a) Push_element(CourseStack,newCourse): To push an object containing the Course_name, Fees, and
Duration of a course, which has fee greater than 100000 to the stack.
(b) Pop_element(CourseStack): To pop the object from the stack and display it. Also, display
"Underflow" when there is no element in the stack.
Page 5 of 12
(c) Peek_element(CourseStack): This function displays the topmost course record from the stack without
delete it. If the stack is empty display message “Empty Stack”
For Example:
If the lists of courses details are:
["MCA", 200000, 3]
["MBA", 500000, 2]
["BA", 100000, 3]
The stack should contain:
["MCA", 200000, 3]
["MBA", 500000, 2]
Ans:
Course=[["MCA", 200000, 3],
["MBA", 500000, 2],
["BA", 100000, 3]]
Univ=[]
def Push_element(Univ,Course):
for L in Course:
if L[1]>100000:
Univ.append([L[0],L[1],L[2]])
def Pop_element(Univ):
if Univ==[]:
print("Stack Empty")
else:
print(Univ.pop())
def peep(Univ):
if Univ==[]:
print("Stack empty")
else:
L=len(Univ)
print(Univ[-1])
def traverse(Univ):
if Univ==[]:
print("Stack empty")
else:
for i in range(len(Univ)-1,-1,-1):
print(Univ[i])
Push_element(Univ,Course)
#Pop_element(travel)
peep(Univ)
traverse(Univ)
OR
Write separate user defined functions for the following:
(a) PUSH(N) : This function accepts a list of names, N as parameter. It then pushes only those names in
the stack named OnlyA which contain the letter 'A'
(b) POPA(OnlyA) : This function pops each name from the stack OnlyA and displays it. When the stack
is empty, the message "EMPTY" is displayed.
(c) Traverse(OnlyA) : Display all elements. If the stack is empty display message “Empty Stack”
For Example:
Page 6 of 12
If the names in the list N are
['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']
Then the stack OnlyA should store
['ANKITA','ANWAR','HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY
Ans:
N=['ANKITA','NITISH','ANWAR','DIPLE','HARKIRAT']
OnlyA=[]
def Push(OnlyA):
for L in N:
if 'A' in L:
OnlyA.append([L])
def PopA(OnlyA):
if OnlyA==[]:
print(" Empty")
else:
print(OnlyA.pop())
def peep(OnlyA):
if OnlyA==[]:
print(" empty")
else:
L=len(OnlyA)
print(OnlyA[-1])
def traverse(OnlyA):
if OnlyA==[]:
print("Empty Stack")
else:
for i in range(len(OnlyA)-1,-1,-1):
print(OnlyA[i])
Push(OnlyA)
#Pop_element(travel)
peep(OnlyA)
traverse(OnlyA)
31.Predict the output of the following code snippets:
d1={1:5, 2:8, 3:12, 4:3, 5:6}
x=y=d1[1]
for k in d1:
if(d1[k] > x):
x= d1[k]
else:
if(d1[k] < y):
y = d1[k]
print(x)
print(y)
Ans : 12
3
OR
str = “Computer Science”
length = len(str)
Page 7 of 12
for a in range(0, length, 2):
print(str[a:a+2])
Ans:
Co
mp
ut
er
S
ci
en
ce
SECTION D
32 Consider the table MobileMaster 4
M_Id M_Company M_Name M_Price M_Mf_Date
MB001 Samsung Galaxy 13000 2014-02-12
MB002 Nokia N1100 7500 2011-04-15
MB003 Realme C35 12000 2021-11-20
MB004 Oppo SelfieEx 12500 2015-08-21
Based on the data given above answer the following questions:
(i) Identify the most appropriate column, which can be considered as Primarykey.
Ans: M_Id-Primary Key
(ii) If two columns are added and 2 rows are deleted from the table result, what will be the new degree
and cardinality of the above table?
Ans: degree/columns- 5+2=7 cardinality-4+2=6
(iii) Write the statements to:
a. To display details of those mobiles whose price is greater than 8000.
Ans: Select * from MobileMaster from M_price>8000;
b. Increase the M_Price of all mobiles manufactured after 2015-01-01 by 500.
Ans: Update MobileMaster set M_Price=M_Price+500 where M_Mf_Date= ‘2015-01-01’;
OR
Write the output for SQL queries (i) to (iv), which are based on the table: STUDENT given below:
(i) SELECT COUNT(*), City FROM STUDENT GROUP BY CITY HAVING COUNT(*)>1;
Ans:
Count(*) City
2 Mumbai
2 Delhi
2 Moscow
Page 8 of 12
08-12-1995 07-05-1993
(iii) SELECT NAME,GENDER FROM STUDENT WHERE CITY=”Delhi”;
Ans:
NAME GENDER
Sonal F
Store m
(iv) SELECT DISTINCT Class FROM STUDENT;
Ans:
Class
X
XII
XI
33 Mr. Snehant is a software engineer working at TCS. He has been assigned to develop code for stock 4
management; he has to create a CSV file named stock.csv to store the stock details of different products.
The structure of stock.csv is : [stockno, sname, price, qty], where stockno is the stock serial number (int),
sname is the stock name (string), price is stock price (float) and qty is quantity of stock(int).
Mr. Snehant wants to maintain the stock data properly, for which he wants to write the following user-
defined functions:
AcceptStock() – to accept a record from the user and append it to the file stock.csv. The column headings
should also be added on top of the csv file. The number of records to be entered until the
user chooses ‘Y’ / ‘Yes’.
StockReport() – to read and display the stockno, stock name, price, qty and value of each stock as
price*qty. As a Python expert, help him complete the task.
Ans:
import csv
def AcceptStock():
fw=open("Stock.csv", "w", newline= "")
writer=csv.writer(fw)
stock=[]
writer.writerow(["stockno", "sname", "price", "qty"])
while True:
stockno=int(input("Enter Stock No : "))
sname=input("Enter Stock Name : ")
price=float(input("Enter Stock Price : "))
qty=int(input("Enter Quantity : "))
data=[stockno,sname,price,qty]
stock.append(data)
ch=input("Add more record Y/N").upper()
if ch=='N':break
writer.writerows(stock)
fw.close()
def StockReport():
fr=open("Stock.csv",'r')
reader=csv.reader(fr,delimiter=',')
for row in reader:
print(row)
AcceptStock()
StockReport()
Page 9 of 12
34 4
(ii) Mr. Shuvam wants to write a program in Python to Display the following record in the table
named EMP in MYSQL database EMPLOYEE with attributes:
•EMPNO (Employee Number ) - integer •
•ENAME (Employee Name) - string size(30)
• SAL (Salary) - float (10,2)
• DEPTNO(Department Number) - integer
Note: The following to establish connectivity between Python and MySQL:
• Username - root • Password – admin • Host – localhost
The values of above fields. Help Shivam to write the program in Python.
Page 10 of 12
Ans:
import mysql.connector
Mydb=mysql.connector.connect(host=”localhost”, user= “root”, passwd= “admin”, databse=
“Employee”)
Mycur=Mydb.cursor()
Empno=int(input(“Employee Number ” ))
EName=input(“Employee Name”)
Sal=float(input(“Salary”))
Deptno=int(input(“Department Number”)
Data=(Empno,Ename,Sal,Deptno)
Sql=”Insert into EMP values(%s,%s,%s,%s)
Mycur.execute(Sql,Data)
Mydb.commit()
Mycur.close()
Mydb.close()
SECTION E
36 A binary file “product.dat” has structure [PNO, PNAME, BRAND_NAME, PRICE] to maintain and 5
manipulate Product details.
*Write a user defined function CreateFile() to input data for a record and add to “product.dat” file. The
number of records to be entered until the user chooses ‘Y’ / ‘Yes’.
*Write a function CountData(BRAND_NAME) in Python which accepts the brand name as parameter
and count and return number of products by the given brand are stored in the binary file “product.dat”.
Ans:
import pickle
def createFile():
fw=open("product.dat",'wb')
prod=[]
while True:
PNO=input("Product No. : ")
PNAME=input("Product Name : ")
BRAND_NAME=input("Brand Name : ")
PRICE=input("Price : ")
data=[PNO, PNAME, BRAND_NAME, PRICE]
prod.append(data)
ch=input("Do you want to add more record? Y/N").upper()
if ch=='N': break
pickle.dump(prod,fw)
fw.close()
def CountData(BRAND_NAME):
fr=open("product.dat",'rb')
count=0
try:
while True:
prod=pickle.load(fr)
print(prod)
print()
for i in prod:
if i[2]=='ABC':
count=count+1
except:
fr.close()
print(BRAND_NAME, "Total published book ",count)
Page 11 of 12
createFile()
CountData('ABC')
37 Alpha Pvt Ltd is setting up the network in Chennai. There are four blocks- Block A,Block B, Block C & 5
Block D.
Page 12 of 12