0% found this document useful (0 votes)
543 views

Assignment 5

The document contains multiple code snippets and questions about Python functions. It asks the reader to trace code, determine function calls, find errors, and predict function returns. Some key points covered are: defining functions with default arguments, global vs local scope, passing arguments by reference vs value, return statements, and function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
543 views

Assignment 5

The document contains multiple code snippets and questions about Python functions. It asks the reader to trace code, determine function calls, find errors, and predict function returns. Some key points covered are: defining functions with default arguments, global vs local scope, passing arguments by reference vs value, return statements, and function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1: Trace the following code and what is the output of the following code:

a) def power(b,p): b=3 p=3

y=b**p y =3**3 =27

return y

def calcsquare(x):

a=power(x,2) a=power(5,2) a=25

return a

n=5

result=calcsquare(n)+power(3,3) 25 + 27= 52

print(result)

b) def addEm(x,y,z):

print(x+y+z) 48

def prod(x,y,z):

return x*y*z

a=addEm(6,16,26) a=none

b=prod(2,3,6) b=36

print(a,b) none 36

c) def increase(x):

a=a+x

return

a=20

b=5

increase(b)

print(a)

d) def func(message,num):
print(message*num)

func(‘Python’,4)

func(‘easy’,3)

e) def check(n1=1,n2=2):

n1=n1+n2

n2+=1

print(n1,n2)

check()

check(2,1)

check(3)

f) a=1

def f():

a=10

print(a)

g) def interest(p,t=2,r=0.10):

return(p*t*r)

print(interest(6100,1))

print(interest(5000,r=0.05))

print(interest(5000,3,0.12))

print(interest(t=4,p=5000))

h) num=1

def myfunc():

return num

print(num)

print(myfunc())
print(num)

i) num=1

def myfunc():

num=10

return num

print(num)

print(myfunc())

print(num)

j) num=1

def myfunc():

global num

num=10

return num

print(num)

print(myfunc())

print(num)

k) def display():

print(“Hello”,end=’ ’)

display()

print(“there”)

l) a=10;

y=5;

def myfunc():

y=a

a=2
print(y,a)

print(a+y)

return a+y

print(y,a)

print(myfunc())

print(y,a)

m)def multiply(number1,number2):

answer=number1*number2

print(number1,’times’,number2,’=’,answer)

return(answer)

output=multiply(5,5)

n) def multiply(number1,number2):

answer=number1*number2

return(answer)

print(number1,’times’,number2,’=’,answer)

output=multiply(5,5)

o) def increment(n):

n.append([4])

return n

L=[1,2,3]

M=increment(L)

print(L,M)

p) def increment(n):

n.append([49])

return n[0],n[1],n[2],n[3]
L=[23,35,47]

m1,m2,m3,m4=increment(L)

print(L)

print(m1,m2,m3,m4)

print(L[3]==m4)

Q2: Find which function calls are correct :

Consider the following function header :

def info(object,spacing=10,collapse=1):

a) info(obj1)

b) info(spacing=20)

c) info(obj2,12)

d) info(obj11,object=obj12)

e) info(obj3,collapse=0)

f)info()

g)info(collapse=0,obj3)

h)info(spacing=15,object=obj4)

Q3: Consider the following and tell which are correct:

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

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

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

d)def func(a=1,b=1,c=2,d):

Q4: What happens in the following code , will the values be swapped ?

def switch(x,y):

x,y=y,x

print(x,y)
x=5

y=7

print(x,y)

switch(x,y)

print(x,y)

Q5: Find the errors in the following code:

a) total=0;

def sum(arg1,arg2):

total=arg1+arg2;

print(total)

return total;

sum(10,20);

print(total)

b) def Tot(Number)

sum=0

for c in range (1,Number+1):

sum+=c

RETURN sum

print(Tot[3])

print(Tot[6])

c)def addem(x,y,z):

return x+y+z

print(x+y+z)

d) def minus(total,decrement)

output=total-decrement
print(output)

return(output)

e) define check()

N=input(‘Enter N: ‘)

I=3

answer=1+i**4/N

Return answer

f) def alpha(n,string=’xyz’,k=10):

return beta(string)

return n

def beta(string)

return string==str(n)

print(alpha(“Valentines day “):)

print(beta(string=’true’))

print(alpha(n=5,”Good-bye”): )

Q6:What will the following function return :

a) def addem(x,y,z):

print(x+y+z)

b) def addem(x,y,z):

return x+y+z

print(x+y+z)

You might also like