Sample Xii Comp SC Hy-2024
Sample Xii Comp SC Hy-2024
SECTION A
1. State True/False 1
A for loop can contain another for loop; a while loop cannot contain another while loop.
2. Evaluate the following Python expressions : 1
6 < 12 and not (20 > 15) or (10 > 5)
3. What shall be the ouput for the execution of the following Python Code ? 1
Cities = ['Delhi', 'Mumbai']
Cities[0], Cities[1] = Cities[1], Cities[0]
print(Cities)
(A) [‘Delhi’,’Mumbai’] (B) [‘Mumbai’,’Delhi’] (C) Error (D) [‘Delhi’,’Delhi’]
4. Which of the following options is/are not Python Keywords ? 1
(A) False (B) Math (C) WHILE (D) breakRevision
5.What possible output(s) is/are expected to be displayed on the screen at the time of execution of
the program from the following code ? 1
import random
Signal = [ 'Stop', 'Wait', 'Go' ]
for K in range(2, 0, -1):
R = random.randrange(K)
print (Signal[R], end = ' # ')
(A) Stop # Wait # Go # (B) Wait # Stop # (C) Go # Wait # (D) Go # Stop #
6. A List is declared as L = ['ONE', 'TWO', 'THREE']. What will be the output of the statement ? 1
print(max(L))
(A) ‘ONE’ (B) ‘THREE’ (C) ‘TWO’ (D) 5
7. For a string S declared as S = 'PYTHON', which of the following is incorrect? 1
(A) N=len(S) (B) T = S (C) 'T' in S (D) S[0] = 'M'
8. Given the dictionary D ={'Rno': 1, 'Name':'Suraj'} , write the output of : 1
print(D['Name'])
9. Write the output of the following Python code : 1
def Update(X=10):
X += 15
print('X = ', X)
X=20
Update()
print('X = ', X)
Hy/COMP.SC/XII Page 1 of 6
10. If a function is defined by the line "def calculate(p, q=100, r=10):", which of the following is
true? 1
a. p is an optional parameter b. q and r are optional parameters
c. q will always have value 100 in the function
d. the above line will cause a syntax error
11. Which of these points about the return statement is FALSE? 1
a. A return statement can only be used inside a function
b. A return statement can be used without any expression
c. When encountered, a return statement terminates a function
d. A function cannot have more than one return statements
12. Identify the statement(s) from the following options which will raise TypeError exception(s): 1
(A) print('5' * 3) (B) print( 5 * 3) (C) print('5' + 3) (D) print('5' + '3')
13. When setting the current position of a file, the position CANNOT be set with reference to which
of these? 1
(A) the beginning of the file (B) the current file position
(C) the middle of the file (D) the end of the file
14. A CSV file 1
(A) is a text file (B) can store images
(C) is a type of python program (D) is a Computer Software Validation file
15. Which of these statements about text and binary files is true? 1
a. A text file has the same number of characters in each line unlike a binary file.
b. A text file has a special End Of File character unlike a binary file.
c. An HTML file is an example of a text file while a CSV file is an example of a
binary file.
d. Every character in a text file can occur in a binary file but the reverse is not true.
16. which of the following functions can't be used for deletion from a stack ST? 1
(A) ST.pop() (B) ST.pop(-1) (C) del(ST[-1]) (D) ST.remove(-1)
17. What possible outputs will be obtained when the following code is executed?
import random
SIDES=["EAST","WEST","NORTH","SOUTH"];
N=random.randint(1,3)
OUT=" "
for i in range(N,1,-1):
OUT=OUT+SIDES[i]
print (OUT )
(a) SOUTHNORTH (b) SOUTHNORTHWEST
(c) WESTNORTHSOUTH (d) EASTWESTNORTH
18. Choose the correct output for the following sequence of operations
Push(5) , push(8) , pop, push(2) , push(5) , pop ,push(1)
85251 (b) 8 5 5 2 1 (c) 2 5 5 1 (d) 5 2 1
19. The ______ block is executed irrespective of an expression is raised or not.
Q20 and 21 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
22. (i)Differentiate between ‘‘w’’ and ‘‘r’’ file modes used in Python. Illustrate the difference
using suitable examples. 1
(ii) How are seek and tell function different from each other? 1
23. Write the output for the execution of the following Python code : 2
def change(A):
S=0
for i in range(len(A)//2):
S+=(A[i]*2)
return S
B = [10,11,12,30,32,34,35,38,40,2]
C = change(B)
24. a) Explain the use of positional parameters in a Python function with the help of a suitable
example. 2
OR
b) Explain the use of a default parameter in a Python function with the help of a suitable example.
25. Rewrite the following code in Python after removing all syntax error(s) : 2
Underline each correction done in the code.
Runs = ( 10, 5, 0, 2, 4, 3 )
for I in Runs:
if I=0:
print(Maiden Over)
else
print(Not Maiden)
26. Write the appropriate statement to call the function/method for the following: 1+1=2
(i)a function which when executed upon 5.8 as parameter, would return the nearest smaller
integer 5
(ii)a function which returns a single number (6) from a given range of numbers (1,6)
27. Find and write the output of the following Python code : 2
def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i] //= 5
if M[i]%3 == 0:
M[i] //= 3
L=[25,8,75,12]
ChangeVal(L,4)
for i in L :
print(i, end='#')
28. Find and write the output of the following Python code : 2
def Call(P=40,Q=20):
P=P + Q
Q=P - Q
print(P,'@',Q)
return P
R=200
S=100
R=Call(R,S)
print (R,'@',S)
S=Call(S)
print(R,'@',S)
Hy/COMP.SC/XII Page 3 of 6
SECTION C
29. Write a program which accepts a list L of integers and displays the sum of all such integers from
the list L which end with the digit 3. 3
For example, if the list L is passed [ 123, 10, 13, 15, 23]
then the function should display the sum of 123, 13, 23, i.e. 159 as follows :
Sum of integers ending with digit 3 = 159
30. Consider the code given below and fill in the blanks. 1*3=3
try:
num1= int(input (" Enter the first number")
num2=int(input("Enter the second number"))
quotient=(num1/num2)
print ("Both the numbers entered were correct")
except _____________: # Statement 1
print (" Please enter only numbers")
except ____________: # Statement 2
print(" Denominator should not be zero")
else:
print(" Great .. you are a good programmer")
___________: #
print(" JOB OVER... GO GET SOME REST")
i) Write Statement 1 to handle errors if numbers are not input
ii) Write Statement 2 to handle errors if Denominator is zero
iii) Write Statement 3 which gets executed irrespective of exception is there or not there.
31. Write the definition of a function ChangeGender() in Python, which reads the contents of a
text file "BIOPIC.TXT" and displays the content of the file with
every occurrence of the word 'he' replaced by 'she'. For example, if the content of the file
"BIOPIC.TXT" is as follows : 3
Last time he went to Agra,
there was too much crowd, which he did not like.
So this time he decided to visit some hill station.
The function should read the file content and display the output as follows :
Last time she went to Agra,
there was too much crowd, which she did not like.
So this time she decided to visit some hill station.
OR
Write the definition of a function Count_Line() in Python, which should read
each line of a text file "Chatrapati.TXT" and count total number of lines present in
text file. For example, if the content of the file is as follows :
Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
India at that time was under Muslim rule.
The function should read the file content and display the output as follows :
Total number of lines : 3
SECTION D
32. Consider the following code and answer accordingly:
def POPSTACK(L): # L is a stack implemented by a list of numbers.
if ________: # Statement 1
print('No elements')
else:
val = _________ # Statement 2
__________ # Statement 3
Stk = [10,20,30]
print(____________) # Statement 4
Hy/COMP.SC/XII Page 4 of 6
i) Write Statement 1 to check for empty stack 1
ii) Write statement 2 to delete one element from the stack 1
iii) Write statement 3 to return the value deleted from the stack. 1
iv) Write a statement 4 to call POPSTACK() 1
33. (i) Differentiate between ‘w+’ and ‘a+’ file opening modes. 1
(ii) Write a function Show_words() in Python to read the content of a text file 'NOTES.TXT' and
display only such lines of the file which have exactly 5 words in them. Example, if the file contains:
3
This is a sample file.
The file contains many sentences.
But needs only sentences which have only 5 words.
Then the function should display the output as :
This is a sample file.
The file contains many sentences :
34. Anwesha of Class 12 is writing a program in Python for her project work to create a CSV file
"Teachers.csv" which will contain information for every teacher's Identification Number, Name for
some entries. She has written the following code. However, she is unable to figure out the correct
statements in a few lines of the code, hence she has left them blank. Help her to write the statements
correctly for the missing parts in the code.
import _________ # Line 1
def addrec(Idno, Name): # to add record into the CSV file
f=open("Teachers.csv", _________) # Line 2
Filewriter = CSV.writer(f)
Filewriter.writerow([Idno,name])
f.close()
def readfile(): # to read the data from CSV file
f=open("Teachers.csv", ________) # Line 3
FileReader = CSV.________ (f) # Line 4
for row in FileReader:
print(row)
f._________ # Line 5
SECTION E
36. (i) Write any two characteristics of a Stack. 1+4=5
(ii)Write the definition of a function POP_PUSH(LPop, LPush, N) in Python. The
function should Pop out the last N elements of the list LPop and Push them into
the list LPush. For example :
If the contents of the list LPop are [10, 15, 20, 30]
And value of N passed is 2,
then the function should create the list LPush as [30, 20]
And the list LPop should now contain [10, 15]
Hy/COMP.SC/XII Page 5 of 6
NOTE : If the value of N is more than the number of elements present in
LPop, then display the message "Pop not possible".
37. A dictionary contains the name and index number (key:value pairs) of some students as given
below:
reg = {"Deepak":"CB/12/024","Mukesh":"CB/12/062",
"Komal":"CB/12/71","Vikash": "CB/12/088"}
Write python statements to perform the following task on the dictionary reg:
A) To access and display all the keys from the dictionary 1
B) To access and display all the values from the dictionary 1
C) To access and display all the key value pairs 1
D) To remove a key value pair "Mukesh":"CB/12/062" 1
E) To change the index number of “Komal” to “CB/12/17” 1
Hy/COMP.SC/XII Page 6 of 6