Functions-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

FUNCTIONS – WORKSHEET 1

1. Rao has written a code to input a number and check whether it is prime or not. His code is
having errors. Rewrite the correct code and underline the corrections made.
def prime():
n=int(input("Enter number to check :: ")
for i in range (2, n//2):
if n%i=0:
print("Number is not prime \n")
break
else:
print("Number is prime \n’)

2. Predict the output of the Python code given below:


def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32] 22 # 40 # 9 # 13 #
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ')

3. Write the output of the code given below:


p=5
def sum(q,r=2):
global p
p=r+q**2 105#6#
print(p, end= '#')
a=10
b=5
sum(a,b)
sum(r=5,q=1)

4. Predict the output of the code given below:


s="welcome2cs"
n = len(s)
m="" sELCcME&Cc
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m +s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m +s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m +'&' print(m)

5. If a function is defined by the line "def calculate(p, q=100, r=10):", which of the following is
true?
a. p is an optional parameter
b. q and r are optional parameters
c. q will always have value 100 in the function
d. the above line will cause a syntax error

6. Which of these points about the return statement is FALSE?


a. A return statement can only be used inside a function
b. A return statement can be used without any expression
c. When encountered, a return statement terminates a function
d. A function cannot have more than one return statements

7. Python's abs() function returns the absolute value of number passed to it. For example abs(5)
is equal to 5 and abs(-3.1) = 3.1. What will be the value of
abs(3 – abs(-10))
a. 13
b. -13
c. -7
d. 7

8. What will be the output of the program snippet below?


def phone_with_country_code (phone_number, country="India"):
country_codes = {"India": "+91", "Singapore": "+65", "United States": "+1"}
if country not in country_codes:
return("Country is not supported")
return (country_codes[country] + " " + phone_number)
print(phone_with_country_code("9876500001"), "|", phone_with_country_code("203-607-
1232", "United States"))
a. India 9876500001 | United States 203-607-1232
b. +91 9876500001 | +1 203-607-1232
c. 9876500001 | +1 203-607-1232
d. 9876500001 | United States 203-607-1232

9. What will be the output of this program?


p=1
q=6
def change_values():
global p
q=5
p=p+q
return (p)
change_values()
print(p, q)
a. 6 5
b. 1 5
c. 6 6
d. 1 6

10. Which of the following components are part of a function header in Python?
a. Function Name
b. Return Statement
c. Parameter List
d. Both a and c

11. Which of the following function header is correct?


a. def cal_si(p=100, r, t=2)
b. def cal_si(p=100, r=8, t)
c. def cal_si(p, r=8, t)
d. def cal_si(p, r=8, t=2)

12. Which of the following is the correct way to call a function?


a. my_func()
b. def my_func()
c. return my_func
d. call my_func()

13. What will be the output of the following Python code?


def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)
a. 50
b. 0
c. Null
d. None

14. What will be the output of the following code?


def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print(my_func(50),my_func())
a. 100 200
b. 150 300
c. 250 75
d. 250 300

15. What will be the output of the following code?


value = 50
def display(N):
global value
value = 25
if N%7==0:
value = value + N
else:
value = value - N
print(value, end="#")
display(20)
print(value)
a. 50#50
b. 50#5
c. 50#30
d. 5#50#

16. What is the output of the following code snippet?


def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")
a) 5#8#15#4#
b) 5#8#5#4#
c) 5#8#15#14#
d) 5#18#15#4#

17. Find and write the output of the following Python code:
def Display(str):
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper() fUNnpYTHON
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
Display('Fun@Python3.0')

18. Find and write the output of the following python code:
def fun(s):
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower() SCHOOLbbbbCOM
elif s[i].isalpha():
m=m+s[i].upper()
else: m=m+'bb'
print(m)
fun('school2@com')

19. Find and write the output of the following python code:
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)
250 # 150
250 # 100
130 # 100
20. Find and write the output of the following python code:
a=10
def call():
global a
a=15
b=20 15
print(a)
call()

21. Consider the code given below:


b=100
def test(a):
------------------- #missing statement
b=b+a
print(a,b)
test(10)
print(b)
Which of the following statements should be given in the blank for #Missing Statement, if the
output produced is 110?
Options:
a. global a
b. global b=100
c. global b
d. global a=100

22. The code given below accepts a number as an argument and returns the reverse number.
Observe the following code carefully and rewrite it after removing all syntax and logical errors.
Underline all the corrections made.
define revNumber(num):
rev=0
rem=0
While num>0:
rem= =num%10
rev=rev*10 +rem
num=num//10
return rev
print(revNumber(1234))

23. Predict the output of the following code:


def Changer(P,Q=10):
P=P/Q
Q=P%Q
return P
A=200
B=20
A=Changer(A,B)
print(A,B,sep=’$’)
B=Changer(B)
print(A,B, sep=’$’, end=’###’)

24. Which of the following components are part of a function header in Python?
a. Function Name
b. Return Statement
c. Parameter List
d. Both a and c

You might also like