Functions 1
Functions 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’)
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
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
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
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()
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))
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