Cs HHW
Cs HHW
Holiday Homework
Computer Science
Class – XII
Instructions:
Write the answers on A4 size paper or loose Register sheets
1. What will be the output of the following python program?
def display:
print(“program”)
def main():
print(“This is python”)
display();
main()
2. What will be the output of the following python program?
def add(j):
if(j>=4):
j = j*j
return j
else:
j= j*2;
return j
def main():
i=4
a=add(4)
print(“The value of a is: %d” %a)
main()
3. What will be the output of the following python program?
i = 100
def abc():
i=8
print(“First = %d” %(i))
def main():
1
i=2
abc()
print(“Second = %d” %(i))
main()
7. Carefully observe the following python code and answer the questions
that follow:
x=5
def fun2():
x=3
global x
x = x+1
print x
2
print x
On execution the above code produces the following output
6
3
8. Consider the following code :
import random
print (int (20+random.random( ) * 5),end= ‘ ‘)
print (int (20+random.random( ) * 5),end= ‘ ‘)
print (int (20+random.random( ) * 5),end= ‘ ‘)
print (int (20+random.random( ) * 5))
Find the suggested output from options (i) to (iv) . Also write the least
value and highest value that can be generated.
(i) 20 22 24 25 (ii) 22 23 24 25 (iii) 23 24 23 24 (iv) 21 21 21 21
9. Observe the following python code and find out which out of the given
options (i) to (iv) are the expected outputs. Also assign the maximum
and minimum value that can be assigned to the variable ‘Go’
import random
X = [100,75,10,125]
Go = random.randint(0,3)
for i in range(Go):
print(X[i], “$$”, end = “”)
(i) 100 $$75 $$10
(ii) 75$$10$$125$$
(iii) 75$$10$$
(iv) 0$$125$$100
10.Observe the following program and answer the questions that follow:
import random
X=3
N = random.randint(1,X)
for i in range(N):
print(i, ‘#’, i+1)
(a) What is the maximum and minimum number of times the loop will
execute?
(b) Find out which line of outputs out of (i) to (iv) will not be executed
from the program
(i) 0#1
3
(ii) 1#2
(iii) 2#3
(iv) 3#4
11.Rewrite the following code in python after removing all syntax errors.
Underline each correction done in the code
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
12.Rewrite the following code after removing the syntactical errors (if any).
Underline each correction
def chksum:
x = input(“Enter a number:”)
if (x%2 = 0):
for i range(2*x):
print i
loop else:
print “#”
13.Correct the following code:
def count_to_ten():
for i in range[10]:
print(i)
count_to_ten()
14.Find out the error in the following program and write the correct line of
code:
def sum(arg1,arg2):
total = arg1+arg2;
print(“Inside the function local total:”,total)
return total;
def main():
n1 = int(input(“First value is:”))
4
n2 = int(input(“Second value is:”))
sum(n1)
print(“The sum is:”,total)
main()
15.From the program code given below, identify the parts mentioned
below:
def processNumbers(x):
x = 72
return x+3
y = 54
res = processNumber(y)
Identify these parts: function header, function call, arguments, parameters,
function body, main program
Practical File
Write the following programs and their outputs in the practical file:
1 Program to get roll numbers, names and marks of the students of a class from user
and store these details in a file called “Marks.txt”
2 Program to read a text file line by line and display each word separated by a ‘#’.
3 Program to read a text file and display the number of vowels/ consonants/
uppercase/ lowercase characters in the file.
4 Program to get student roll no., name and marks from user and write onto a binary
file.
5 Program to append student record to a binary file by getting data from user.
6 Program to create a binary file with name and roll number. Search for a given roll
number and display the name, if not found display appropriate message.
7 Program to create a binary file Stu.dat storing student details and update the
records of the file so that those who have scored more than 81 marks, get additional
bonus of 2 marks
8 Program to create a binary file with roll number, name and marks. Input a roll
number and update the marks.
9 Program to remove all the lines that contain the character `a' in a file and write it
to another file.
5
10 A binary file “STUDENT.DAT” has structure (admission_number, name ,
percentage).Write a function CountRec( ) in Python that would read contents of the
file “STUDENT.DAT” and display the details of those students whose percentage is
above 75. Also display number of students scoring above 75%.
15 Program to modify CSV file so that blank lines for EOL character are not
displayed.
16Take a sample of any text file and find most commonly occurring word(s)
Project
The aim of the class project is to create something that is tangible and useful
using Python / Python and SQL connectivity. This should be done in groups of two
or three students . The aim here is to find a real world problem that is worthwhile to
solve.
For example, if a business is finding it hard to create invoices for filing GST claims,
then you can do a project that takes the raw data (list of transactions), groups the
transactions by category, accounts for the GST tax rates, and creates invoices in the
appropriate format. You can be extremely creative here. You can use a wide
variety of Python libraries to create user friendly applications. Save the project file
with .py extension
6
Activities
1. Create a calculator using Python coding syntax.
2. Write Python code to create Tic Tac Toe game
3. Create alarm clock using Python coding technique
PROGRAM-11
q=[]
c="y"
while c=="y":
print("1.Insert")
print("2.Delete")
print("2.Display")
choice=int(input("Enter your choice:"))
if choice==1:
a=input("Enter any number:")
q.append(a)
elif choice==2:
if q==[]:
print("Queue Empty")
else:
print("Deleted element is:",q.pop(0))
elif choice==3:
l=len(q)
for i in range(0,l):
print(q[i])
else:
7
print("Wrong input")
c=input("Do you want to continue or not?")
output-
1.Insert
2.Delete
2.Display
Enter your choice:1
Enter any number:1
Do you want to continue or not?y
1.Insert
2.Delete
2.Display
Enter your choice:1
Enter any number:2
Do you want to continue or not?y
1.Insert
2.Delete
2.Display
Enter your choice:1
Enter any number:3
Do you want to continue or not?y
1.Insert
2.Delete
2.Display
Enter your choice:2
Deleted element is: 1
Do you want to continue or not?y
8
1.Insert
2.Delete
2.Display
Enter your choice:3
2
3
Do you want to continue or not?n