Documente Academic
Documente Profesional
Documente Cultură
VALASARAVAKKAM
CHENNAI
COMPUTER SCIENCE- HOME TEST I
MAX.MARKS: 35
DATE: 09-04-2020 TIME: 1.30 HRS
1.Find and write the output of the following python code: (3)
def Change(P ,Q=30):
P=P+Q
Q=P-Q
print( P,"#",Q)
return (P)
R=150
S=100
R=Change(R,S)
print(R,"#",S)
S=Change(S)
ANSWER
250 # 150
250 # 100
130 # 100
(1 mark each for correct line)
2. Find and write the output of the following python code: (1)
a=10
def call():
global a
a=15
b=20
print(a)
call()
ANSWER
15
(1 mark for correct answer)
3.What do you understand by local and global scope of variables? How can you
access a global variable inside the function, if function has a variable with same
name.
(2)
ANSWER
A global variable is a variable that is accessible globally. A local variable is one that
is only accessible to the current scope, such as temporary variables used in a single
function definition.
A variable declared outside of the function or in global scope is known as global
variable. This means, global variable can be accessed inside or outside of the
function where as local variable can be used only inside of the function. We can
access by declaring variable as global A.
(1 mark for correct difference)
(1 mark for explanation)
ANSWER
(a)
def check():
N = int(input(“Enter N:”))
i=3
answer = 1 + i ** 4 / N
return answer
(b) The default arguments right to left (Default argument should not precede the
non-default argument)
ANSWER
def average(a,b,c):
return (a+b+c)/3
res=average(65,78,90)
print(res)
6. Write a program using function to compute the square of three numbers x, y and z
and returns the result. The values are passed as parameters to the function. (2)
ANSWER
def square(x,y,z):
Return x**2,y**2,z**2
x=4
y=8
z=7
t1,t2,t3 =square(x,y,z)
print(t1,t2,t3)
ANSWER
def si(p,n,r):
return p*n*r/100
ANSWER
ANSWER
a) fabs() – math b)randint() -random
10. What possible outputs(s) are expected to be displayed on screen at the time of
execution of the program from the following code? Also specify the maximum
values that can be assigned to each of the variables BEGIN and LAST. (2)
import random
POINTS=[30,50,20,40,45]
BEGIN=random.randint(1,3)
LAST=random.randint(2,4)
for C in range(BEGIN,LAST+1):
print (POINTS[C],"#")
ANSWER
MAX VALUE FOR BEGIN 3 (1/2)MARK
MAX VALUE FOR LAST 4 (1/2)MARK
OPTION (ii) AND (iii) (1) MARK
11. Write a function zeroending(scores) to add all those values in the list of scores
which are ending with 0,(zero) and display the sum. (3)
For example if the scores contain [200,456,300,100,234,678] it should display 600.
ANSWER
def zeroending(scores):
sum =0
for i in scores:
if i %10 ==0:
sum = sum + i
print(sum)
12. Write a function countnow(places) to find and display those places in which
there are more than 5 characters. (2)
ANSWER
def countnow(places):
for i in places:
if len(i) > 5:
print(i)
13. Find and write the output of the following python code : (3)
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)
B=Changer(B)
print (A,"$",B)
A=Changer(A)
print (A,"$",B)
ANSWER
10.0 # 10.0
10.0 $ 20
2.0 # 2.0
10.0 $ 2.0
1.0 # 1.0
1.0 $ 2.0
14. Write a program using function to accept a number (n) as a parameter and find
the sum of first n natural numbers. (2)
ANSWER
def sumcalc(n):
sum = 0
for i in range(1,n+1):
sum = sum + i
return sum
n=25
sumcalc(25)
ANSWER
b) Function name and parameter list
a)120 b)Nothing is printed c)Error, factorial does not exist in math module
d) Error, the statement should be print(factorial(5))
17. What is the difference between parameter and an argument in a function? (1)
def area(r):
return 3.14 *r *r
area(3)
Identify which is a parameter and which is an argument?
18. What are the three types of arguments in functions? Illustrate with examples.(2)
ANSWER: