Sunteți pe pagina 1din 6

DEVI ACADEMY SENIOR SECONDARY SCHOOL

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)

4. Find the errors in code given below: (2)

(a) define check()


N = input(“Enter N:”)
i=3
answer = 1 + i ** 4 / N
Return answer

(b) def func(a=1,b):

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)

5. Write a program using function to compute average of three subjects. (2)

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)

7. Create a module simpleinterest.py that stores function for computing simple


interest. Use the module to compute interest for the given principal, no of years and
rate of interest. (3)

ANSWER
def si(p,n,r):
return p*n*r/100

save this program as simpleinterest.py


import simpleinterest
p=5000
n=7
r=8
res=si(p,n,r)
print(res)

8. What is the use of help() and dir() functions? (2)

ANSWER

help(simpleinterest) will display NAME, FILE(path),FUNCTIONS,DATA


dir(simpleinterest) will display the names of all that is defined inside the module

9. Name the Python library modules to be imported to execute the following


function.
(1)
a) fabs() b)randint()

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],"#")

(i) 20#50#30# (ii) 20#40#45# (iii) 50#20#40# (iv) 30#50#20#

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)

places = [“Delhi”,”London”,”Paris”,”New York”, “Dubai”]

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)

15. Which of the following is present in the function header? (1)


a) function name b) function name and parameter list c) parameter list d)return
value

ANSWER
b) Function name and parameter list

16.What will be the output of the following Python code? (1)


from math import factorial
print(math.factorial(5))

a)120 b)Nothing is printed c)Error, factorial does not exist in math module
d) Error, the statement should be print(factorial(5))

ANSWER option (d)

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?

ANSWER: def area(r): r is the parameter (in the function header-parameter)

area(3) - 3 is the argument(function call)

18. What are the three types of arguments in functions? Illustrate with examples.(2)

ANSWER:

a) Default arguments: def fun(a,b=5,c=2): Default values given in the function


header will be used if the arguments are not provided.
b) Keyword (Named) arguments: fun(b=6,a=20,c=3) Giving the values through
names
c) Positional arguments: fun(2,4,5) a=2,b=4,c=5 will be taken.

S-ar putea să vă placă și