Xii CS 2020-21
Xii CS 2020-21
SESSION : 2020-21
Class : XII
Subject : Computer Science (Python)
HALF YEARLY EXAMINATION
Time Allowed : 180Minutes Max Marks : 50 Min Marks : 17
Read the question carefully before attempt?
All questions are compulsory?
Computer Science – Python Revision Tour – I and II, Function, File Handling, Data Structure
Q.1 (2m each)
a) Find and write the output of the following Python code:
L = ["X",20,"Y",10,"Z",30]
CNT = 0
ST = ""
INC = 0
for C in range(1,6,2):
CNT= CNT + C
ST= ST + L[C-1] + "@"
INC = INC + L[C]
print(CNT, INC, ST)
b) Write the output of the following Python program code:
A = [10,12,15,17,20,30]
for i in range(0,6):
if (A[i] % 2 == 0):
A[i] /= 2
elif (A[i] % 3 == 0):
A[i] /= 3
elif (A[i] % 5 == 0):
A[i] /= 5
for i in range(0,6):
print(A[i],end= "#")
c) What are the possible outcomes executed from the following code? Also, specify the
maximum and
minimum values that can be assigned to variable COUNT.
import random
TEXT = "CBSEONLINE"
COUNT = random.randint(0,3)
C=9
while TEXT[C] != 'L':
print(TEXT[C]+TEXT[COUNT]+'*',end=" ")
COUNT= COUNT + 1
C = C-1
i) EC* NB* IS*
ii) NS* IE* LO*
iii) ES* NE* IO*
iv) LE* NO* ON*
d) Write the output of the following Python program code:
colors=["violet", "indigo", "blue", "green", "yellow", "orange", "red"]
del colors[4]
colors.remove("blue")
colors.pop(3)
print(colors)
e) What output will be generated when the following Python code is executed?
def ChangeList():
L=[]
L1=[]
L2=[]
for i in range(1, 10):
L.append(i)
for i in range(10,1,-2):
L1.append(i)
for i in range(len(L1)):
L2.append(L1[i]+L[i])
L2.append(len(L)-len(L1))
print(L2)
ChangeList()
Q.2 (2m each)
a) Find the output of the following Python program:
def makenew(mystr):
newstr = ""
count = 0
for i in mystr:
if count%2 != 0:
newstr = newstr + str(count)
else:
if i.islower():
newstr = newstr + i.upper()
else:
newstr = newstr + i
count += 1
newstr = newstr + mystr[:1]
print("The new string is:", newstr)
makenew("sTUdeNT")
b) Observe the following Python code very carefully and rewrite it after removing all
syntactical errors with each correction underlined.
DEF execmain():
x = input("Enter a number:")
if (abs(x)=x):
print ("You entered a positive number")
else:
x=*-1
print "Number made positive:"x
execmain()
c) Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code:
x=integer(input('Enter 1 or 10'))
if x==1:
for x in range(1,11)
Print(x)
Else:
for x in range(10,0,-1):
print(x)
d) Rewrite the following code in python after removing all syntax error(s). Underline each
correction done in the code:
t1=(10,20,30,40,50,60,70,80)
str=””
str=index(t1(40))
print(“index of tuple is ”, str)
str=t1.max()
print(“max item is “, str
e) Write definition of a method/function AddOdd(VALUES) to display sum of odd
values from the list of VALUES.
Q.3 (2m each)
a) Convert the following for loop into a while loop:
Val = int(input("Value:"))
Adder =0
for C in range(1,Val,3):
Adder += C
if C%2 = 0:
print(C*10)
else:
print(C)
print(Adder)
b) Study the following program and select the possible output(s) from options (i) to (iv)
following it. Also, write the maximum and the minimum values that can be assigned to
variable Y.
import random
X= random.random()
Y= random.randint(0,4)
print(int(X),":",Y+int(X))
(i) 0 : 0 (ii) 1 : 6
(iii) 2 : 4 (iv) 0 : 3
c) Write a user defined function in python that displays the number of lines starting with 'H' in
the file test.txt?
d) Write a function countmy() in python to read the text file "test.txt" and count the number of
times "the" occurs in the file.
e) Write a method in python to read lines from a text file DIARY.TXT and display those lines
which start with the alphabets P.
Q.4 (4m,3m,3m)
a) Write PushOn(Book) and Pop(Book) methods/functions in Python to add a new Book and
delete a Book from a list of Book titles, considering them to act as push and pop operations
of the Stack data structure.
b) Obtain the postfix notation for the following infix notation of expression showing the contents
of the stack and postfix expression formed after each step of conversion : (P—Q)/(R*(S—
T)+U).
c) Evaluate the following POSTFIX notation. Show status of Stack after every step of evaluation
(i.e. after each operation)
False NOT, True, AND, True, False, OR, AND
Q.5 (2m each)
a) Write a program in python to check a number whether it is prime or not.
b) Write a program to generate first 15 Fibonacci numbers.
c) Write a program to remove all the lines that contain the character `a' in a file and write it to
another file.
d) Write a function that takes a list of strings as parameter and prints the strings that are
palindromes.
e) Write a program to count number of words in a file.