CS XII - MTP-1 (Solved)
CS XII - MTP-1 (Solved)
General Instructions:
• This question paper contains 37 questions.
• All questions are compulsory. However, internal choices have been provided in some questions. Attempt only
one of the choices in such questions.
• The paper is divided into 5 Sections—A, B, C, D and E.
• Section A consists of 21 questions (1 to 21). Each question carries 1 Mark.
• Section B consists of 7 questions (22 to 28). Each question carries 2 Marks.
• Section C consists of 3 questions (29 to 31). Each question carries 3 Marks.
• Section D consists of 4 questions (32 to 35). Each question carries 4 Marks.
• Section E consists of 2 questions (36 to 37). Each question carries 5 Marks.
• All programming questions are to be answered using Python language only.
• In case of MCQ, text of the correct answer should also be written.
Section A
1. State whether the following statement is True or False: (1)
The arguments of range() function in Python must be integers.
Ans. True
2. Identify the output of the following code snippet: (1)
text = "Hello World from Python"
modified_text = text.title().strip().replace("o", "@").replace(" ", "-")
print(modified_text)
(a) Hell@-W@rld-fr@m-Pyth@n (b) Hell@-W@rld-from-Pyth@n
(c) Hello-World-from-Python (d) Hell@-World-from-Python
Ans. (a) Hell@-W@rld-fr@m-Pyth@n
3. Which of the following expressions evaluates to True? (1)
(a) True and not (False or True) (b) False or (True and False)
(c) True or (False and True) (d) False and (True or False)
Ans. (c) True or (False and True)
4. What will be the output of the given expression? (1)
text = "Hello, welcome to the world of Python!"
print(text.find("world"))
(a) 13 (b) 18
(c) 22 (d) –1
Ans. (c) 22
5. What will be the output of the following code snippet? (1)
course = "AdvancedMachineLearning"
result = course[0:9] + course[10:18]
print(result)
(a) AdvancedMchineLea (b) AdvancedMachineLea
(c) AdvancedMacchineLea (d) AdvancedMachineLearn
Ans. (a) AdvancedMchineLea
6. What will be the output of the following code?(1)
t1 = (1, 2, 3)
t2 = (4, 5)
result = t1 * 2 + t2
print(result)
(a) (1, 2, 3, 1, 2, 3) + (4, 5) (b) (1, 2, 3, 4, 5)
(c) (2, 4, 6, 8, 10) (d) (1, 2, 3, 1, 2, 3, 4, 5)
Ans. (d) (1, 2, 3, 1, 2, 3, 4, 5)
7. If car_info is a dictionary as defined below, then which of the following statements will raise an
exception?(1)
car_info = {'make': 'Toyota', 'model': 'Corolla', 'year': 2020}
(a) car_info['model'] = 'Camry' (b) car_info['year'] = 2021
(c) print(car_info.get('make')) (d) print(car_info['color'])
Ans. (d) print(car_info['color'])
8. Which of the following statements accurately describes the behaviour of the update() method when used
with dictionaries in Python? (1)
(a) It creates a new dictionary by merging the original dictionary with another dictionary or iterable and
returns this new dictionary.
(b) It replaces the values of the keys that already exist in the dictionary with the values from another
dictionary or iterable and adds new key-value pairs if they do not exist in the original dictionary.
(c) It only adds new key-value pairs from another dictionary or iterable to the original dictionary without
modifying any existing key-value pairs.
(d) It removes key-value pairs from the original dictionary that are present in another dictionary or iterable.
Ans. (b) It replaces the values of the keys that already exist in the dictionary with the values from another
dictionary or iterable and adds new key-value pairs if they do not exist in the original dictionary.
9. If a table has three candidate keys and two alternate keys, then how many primary keys will this table
have?(1)
Ans. A table has only one primary key.
10. Write the missing statement to complete the following code: (1)
file = open("records.txt", "r")
content = file.read()
_________________ # Move the file pointer to the beginning of the file
firstline = file.readline()
file.close()
Ans. file.seek(0)
11. State whether the following statement is True or False: (1)
IOError is raised in Python when an index is not found in a sequence, i.e., it is out of range or out of bounds.
Ans. False
12. What will be the output of the following code? (1)
z = 30
def cal():
z = 60
print(z, end="#")
cal()
z = z + 10
print(z, end="@")
(a) 60#70@ (b) 60#30@
(c) 60#40@ (d) 60@40#
Ans. (c) 60#40@
13. Which SQL command is used to add a primary key constraint to an existing table? (1)
Ans. ALTER TABLE command is used to add a primary key constraint to an existing table.
Section B
22. Why are lists called mutable data type? Explain with examples. (2)
Ans. Lists are called mutable data type because the values in a list can be changed or modified and can be accessed
using the index value enclosed within square brackets.
For example:
L = [1,3,6,7,98]
L[2] = 56
The new list is L = [1,3,56,7,98].
Appendices A.37
23. What is implicit conversion in Python? (2)
Ans. Implicit conversion in Python is also known as coercion. This conversion means that the data type conversion
is done automatically during run-time and is not instructed by the programmer.
For example:
x=9
y=99.0
z=x+y
print(z)
So, it gives the output 108.0 as implicit conversion is performed to convert it into float automatically.
24. Answer the following using built-in functions only. (2)
If T1= (3,5,7,9) and T2 = (1,2,4,6), then
I. (a) Write a statement to convert T1 to a list.
OR
(b) Write a statement to find the maximum value from tuple T2.
II. (a) Write a statement to find the sum of all elements in T1.
OR
(b) Write a statement to find the length of T2.
Ans. I. (a) L1 = list(T1)
OR
(b) Max_Value = max(T2)
II. (a) T_Sum= sum(T1)
OR
(b) T2_length = len(T2)
25. Identify the correct output(s) of the following code. Also write the minimum and the maximum possible values
of the variable val. (2)
import random
s =[100,75,10,125]
val =random.randint(0,3)
for j in range(val):
print(s[j],"$$")
(a) 100$$ (b) 100$$
75$$ 99$$
10$$
(c) 150$$ (d) 125$$
100$$ 10$$
Ans. The correct output is:
(a) 100$$
75$$
10$$
The minimum value of variable val is 0 and the maximum value of variable val is 3.
26. The following code is intended to check whether a number is positive or not; if negative, then make it a
positive number. However, there are syntax and logical errors in the code. Rewrite it after removing all
errors. Underline all the corrections made. (2)
DEF check_num():
num = input("Enter a number:")
if (abs(num)= num):
print"You entered a positive number"
else:
num=*-1
print"Number made positive:"num
check_num()
Section C
29. (a) Write a Python function called Replace_ Word(filename, old_word, new_word) that takes three
parameters: filename (string), old_word (string) and new_word (string). The function should replace
all occurrences of old_word with new_word in the specified file. (3)
OR
(b) Write a Python function called Unique_Words(filename) that takes a single parameter, filename (string).
The function should read the content of the specified file and return a set of unique words.
Ans. (a) def Replace_Word_in_file(filename, old_word, new_word):
try:
with open(filename, 'r') as file:
content = file.read()
content = content.replace(old_word, new_word)
with open(filename, 'w') as file:
file.write(content)
except FileNotFoundError:
return "File not found."
Note: Users can take any filename, old word and new word.
Appendices A.39
OR
(b) def Unique_Words(filename):
try:
with open(filename, 'r') as file:
text = file.read()
words = text.split()
return set(words)
except FileNotFoundError:
return "File not found."
Note: Users can take any filename.
30. (a) Consider a dictionary containing names and fees as key-value pairs of 5 doctors. Write a program with
separate user-defined functions to perform the following operations: (3)
(i) Push the keys (name of the doctors) of the dictionary into a Stack where the corresponding value
(fees) is greater than 1000.
(ii) Pop and display the contents of the Stack.
The dictionary should be as follows:
doc={"Reena":500,"Amrita":1000,"Shama":900,"Vishal":1500,"Vyom":2000}
Then the output will be:
Vishal, Vyom
OR
(b) Write functions in Python, do_push(Num) and do_pop(Num), to add a new number and delete a
number from a list of numbers, considering them to act as push and pop operations of the
Stack data structure.
Ans. (a) def push(stk,item):
stk.append(item)
def pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]
doc={"Reena":500, "Amrita": 1000, "Shama":900, "Vishal":1500, "Vyom":2000}
for i in doc:
if doc[i] > 1000:
push(stk,i)
while True:
if stk!=[]:
print(pop(stk),end=" ")
else:
break
OR
(b) def do_push(Num):
a=int(input("Enter a number:"))
Num.append(a)
def do_pop(Num):
if(Num==[]):
print("stack empty")
else:
print("Deleted element",Num.pop())
Section D
32. Consider the given table STUDENT:(4)
Table: STUDENT
Note: The table contains more records than shown here.
(a) Write the following queries:
(i) To display the records in alphabetical order as per the name of the student.
(ii) To display the average marks of Delhi students.
(iii) To display the details of students whose names end with the letter ‘a’.
(iv) To display the total number of students studying in class XI or XII.
OR
(b) Write the output:
(i) Select * from STUDENT where gender = 'F' and marks>350;
(ii) Select RollNo, DOB, City from STUDENT where Marks between 200 and 300;
(iii) Select Min(Marks ) from STUDENT;
(iv) Select Name, Marks from STUDENT where city IN ('Agra', 'Dubai');
Appendices A.41
Ans. (a) (i) Select * from STUDENT order by Name;
(ii) Select avg(Marks) from STUDENT where city = "Delhi";
(iii) Select * from STUDENT where Name like '%a';
(iv) Select count(*) from STUDENT where Class = 'XI' or Class = 'XII';
(b) (i)
RollNo Name Class DOB Gender City Marks
3 Sonal XI 1994-05-06 F Delhi 400
4 Trisla XII 1995-08-08 F Mumbai 450
7 Neha X 1995-12-08 F Moscow 377
(ii)
RollNo DOB City
6 1994-12-12 Dubai
(iii)
Min(Marks)
250
(iv)
Name Marks
Nanda 551
Marisla 250
33. Write user-defined functions to perform read and write operations on to a ‘teacher.csv’ file having fields
TeacherID, Name, Subject and Experience (in years). (4)
Ans. import csv
row=['2101','Amrita Rana', 'Science','15']
def readcsv():
with open("teacher.csv",'r') as f:
data=csv.reader(f)
for row in data:
print(row)
def writecsv():
with open("teacher.csv",'w',newline='') as fobj:
csv_w=csv.writer(fobj,delimiter=',')
csv_w.writerow(row)
print("Press 1 to read data and Press 2 to write data")
a=int(input())
if a==1:
readcsv()
elif a==2:
writecsv()
else:
print("Invalid value")
34. Dhriti works in Prilknot Pvt. Ltd. She needs to access some information from SALESMAN and CUSTOMER tables.
Help her extract the following information by writing the desired SQL queries as mentioned below. (4)
Table: SALESMAN
Salesman_id Name City Commission
5001 Jai Chauhan New York 0.15
5002 Neil Mahajan Paris 0.13
5005 Priyank Chawla London 0.11
5006 Prerna Malhotra Paris 0.14
5007 Jyoti Singh Rome 0.13
5003 Aaren Kumar San Jose 0.12
Field Type
Vehicle_id Int(8)
Vehicle_Name Varchar(20)
Price Float
Quantity Int(10)
Write the following Python function to perform the specified operation:
Show_data() to display and read only those records in which Quantity more than 100.
Note the following to establish connectivity between Python and MySQL:
Host: localhost
Username: root
Password: motors
Ans. import mysql. connector as mysql
def show_data():
con1=mysql.connect(host="localhost", user="root", password="motors",\
database ="MOTORSDB")
mycursor=con1.cursor()
print("Vehicles having quantity more than 100")
mycursor. execute ("Select * from vehicle where quantity > 100")
data=con1.commit()
for i in data:
print(i)
print()
Appendices A.43
Section E
36. Shreya is a manager working in Le Hotels Group of J&G industry. She needs to manage the records of various
hotels. For this, she wants the following information of each hotel to be stored: (5)
Hotel_ID – Integer
Hotel_Name – String
Location – String
No_of _Rooms – Integer
As a programmer of the company, you have been assigned to do this job for Shreya.
(a) Write a function to input the data for a record and add to a binary file.
(b) Write a function in Python which accepts Location as a parameter and counts and returns the number of
hotels by the given location stored in a binary file.
Ans. (a) import pickle
def datafile():
f=open("Hotel.dat","rb")
Hotel_ID=int(input("Enter hotel id"))
Hotel_Name=input("Enter name of the hotel")
Location=input("Enter location of the hotel")
No_of_Rooms=int(input("Enter number of rooms in a hotel"))
record=[Hotel_ID, Hotel_Name, Location, No_of_Rooms]
pickle.dump(record,f)
f.close()
(b) import pickle
def CountRecords(Location):
f=open("Hotel.dat","rb")
count=0
try:
while True:
record=pickle. load(f)
if Location == record[2]:
count=count+1
except:
f. close()
return count
37. Canvo International Inc. is planning to connect its Bengaluru Office Set-up with its Head Office in Delhi. The
Bengaluru Office of Canvo International Inc. is spread across an area of approximately 1 square kilometre,
consisting of 3 blocks: Human Resources, Administration and Academics. As a network expert, you have to
suggest solutions to the following five queries, i.e., (a) to (e), raised by them, keeping in mind the distances
between various blocks and the number of computers in each block. (5)
(a) Suggest the most suitable block in the Bengaluru Office Set-up to host the server. Give a suitable reason.
(b) Suggest a cable layout among the various blocks within the Bengaluru Office Set-up for connecting the
blocks.
(c) Suggest a suitable networking device to be installed in each block, essentially required for connecting
computers inside the blocks with fast and efficient connectivity.
(d) Suggest the most suitable media to provide secure, fast and reliable data connectivity between the Delhi
Head Office and the Bengaluru Office Set-up.
(e) (i) What would be your recommendation for enabling live visual communication between the Bengaluru
Office and the Delhi Head Office from the following options?
I. Videoconferencing
II. Instant Messaging
III. Email
IV. Telephony
OR
(ii) Which type of network (PAN, LAN, MAN or WAN) will be set up among the computers connected in the
Bengaluru Office?
Ans. (a) The most suitable block to host the server is Human Resources because it has the maximum number of
computers.
(b)
Human Resources
65 m 100 m
Academics
Administration
(c) Switch
(d) Satellite link
(e) (i) I. Videoconferencing
OR
(ii) LAN
Appendices A.45